Skip to content

Instantly share code, notes, and snippets.

@CrBoy
Forked from dannvix/paint_text.py
Created March 25, 2012 04:47
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 CrBoy/2191408 to your computer and use it in GitHub Desktop.
Save CrBoy/2191408 to your computer and use it in GitHub Desktop.
Paint text on given image with PIL
#!/usr/bin/env python
#-*- encoding: utf8
# sudo easy_install PIL
import Image
import ImageFont
import ImageDraw
import sys
import os
if len(sys.argv)!=3:
print 'Error!'
exit()
print 'Load: ' + sys.argv[1]
image = Image.open(sys.argv[1])
image_width, image_height = image.size
image_width, image_height = 800, image_height * 800 / image_width
image = image.resize((image_width, image_height), Image.ANTIALIAS)
font_path = '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc';
font = ImageFont.truetype(font_path, 32, encoding='unic')
text = '僅供2012年大工盃主辦單位長榮大學辦理相關手續之用'.decode('utf-8')
text_width, text_height = font.getsize(text)
draw = ImageDraw.Draw(image)
draw.text(((image_width/2 - text_width/2), (image_height/4 - text_height/2)), text, font=font, fill=(180, 180, 180))
draw.text(((image_width/2 - text_width/2), (image_height/2 - text_height/2)), text, font=font, fill=(180, 180, 180))
draw.text(((image_width/2 - text_width/2), (image_height*3/4 - text_height/2)), text, font=font, fill=(180, 180, 180))
image.save(sys.argv[2])
#os.system("eog " + sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment