Skip to content

Instantly share code, notes, and snippets.

@streamride
Forked from pbojinov/text_to_image.py
Created April 13, 2019 09:45
Show Gist options
  • Save streamride/fd5ddf371c9171227d264d2309972394 to your computer and use it in GitHub Desktop.
Save streamride/fd5ddf371c9171227d264d2309972394 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