-
-
Save CrBoy/2191408 to your computer and use it in GitHub Desktop.
Paint text on given image with PIL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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