Closed edouard-lopez closed 7 years ago
From Python image library - font positioning:
from PIL import Image, ImageDraw, ImageFont FULL_SIZE = 100 filename = 'font_posn_test.png' fontname = '/usr/share/fonts/truetype/msttcorefonts/arial.ttf' textsize = 40 text = "5" image = Image.new("RGBA", (FULL_SIZE, FULL_SIZE)) draw = ImageDraw.Draw(image) font = ImageFont.truetype(fontname, textsize) print font.getoffset(text) print font.font.getsize(text) font_width, font_height = font.getsize(text) font_y_offset = font.getoffset(text)[1] # <<<< MAGIC! draw.rectangle(((0, 0), (font_width, font_height)), fill="black") draw.text((0, 0 - font_y_offset), text, font=font, fill="red") image.save(filename, "PNG")
see #4 and #7
From Python image library - font positioning: