parlr / ruby-font-creator

Generate rich Unicode open fonts with custom annotations, transliterations, pronunciations.
21 stars 3 forks source link

Python image creation using font #3

Closed edouard-lopez closed 7 years ago

edouard-lopez commented 8 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")
edouard-lopez commented 7 years ago

see #4 and #7