Skip to content

Instantly share code, notes, and snippets.

@pbojinov
Created March 19, 2015 00:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbojinov/7f680445d50a9bd5a421 to your computer and use it in GitHub Desktop.
Save pbojinov/7f680445d50a9bd5a421 to your computer and use it in GitHub Desktop.
Using Python's PIL to render text to image - http://effbot.org/imagingbook/overview.htm
import os
from PIL import Image, ImageDraw, ImageFont
# make sure you have the fonts locally in a fonts/ directory
georgia_bold = 'fonts/georgia_bold.ttf'
georgia_bold_italic = 'fonts/georgia_bold_italic.ttf'
# W, H = (1280, 720) # image size
W, H = (720, 405) # image size
txt = 'Hello Petar this is my test image' # text to render
background = (0,164,201) # white
fontsize = 35
font = ImageFont.truetype(georgia_bold_italic, fontsize)
image = Image.new('RGBA', (W, H), background)
draw = ImageDraw.Draw(image)
# w, h = draw.textsize(txt) # not that accurate in getting font size
w, h = font.getsize(txt)
draw.text(((W-w)/2,(H-h)/2), txt, fill='white', font=font)
# draw.text((10, 0), txt, (0,0,0), font=font)
# img_resized = image.resize((188,45), Image.ANTIALIAS)
save_location = os.getcwd()
# img_resized.save(save_location + '/sample.jpg')
image.save(save_location + '/sample.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment