Skip to content

Instantly share code, notes, and snippets.

@massenz
Last active September 10, 2019 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save massenz/26e12ce5dd235a85b74a68e37ec8102f to your computer and use it in GitHub Desktop.
Save massenz/26e12ce5dd235a85b74a68e37ec8102f to your computer and use it in GitHub Desktop.
How to load fonts via Python PIL.
# How to load fonts via Python PIL.
# See Stack OF question: https://stackoverflow.com/questions/24085996/how-i-can-load-a-font-file-with-pil-imagefont-truetype-without-specifying-the-ab/41887497#41887497
from PIL import Image, ImageDraw, ImageFont
# sample text and font
unicode_text = u"Arial Font, size 28px"
font = ImageFont.truetype("/Library/Fonts/Arial.ttf", 28, encoding="unic")
# get the line size
text_width, text_height = font.getsize(unicode_text)
# create a blank canvas with extra space between lines
canvas = Image.new('RGB', (text_width + 10, text_height + 10), "orange")
# draw the text onto the text canvas, and use blue as the text color
draw = ImageDraw.Draw(canvas)
draw.text((5,5), unicode_text, 'blue', font)
# save the canvas to a file
canvas.save("unicode-text.png", "PNG")
canvas.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment