Skip to content

Instantly share code, notes, and snippets.

@meoow
Created August 9, 2014 11:44
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 meoow/f8093a1db9305b0bdf52 to your computer and use it in GitHub Desktop.
Save meoow/f8093a1db9305b0bdf52 to your computer and use it in GitHub Desktop.
Convert Text to Image
#!/usr/bin/env python2.7
# coding:utf-8
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import sys
import re
fontSize = 18
charWidth = 60
fontPath = '/System/Library/Fonts/STHeiti Light.ttc'
pixelWidth = charWidth * (fontSize/2+1)
rx=re.compile(u"([\u2e80-\uffff])", re.UNICODE)
def cjkwrap(text, width, encoding="utf8"):
return reduce(lambda line, word, width=width: '%s%s%s' %
(line, [' ','\n', ''][(len(line)-line.rfind('\n')-1
+ len(word.split('\n',1)[0] ) >= width) or
line[-1:] == '\0' and 2], word),
rx.sub(r'\1\0 ', unicode(text,encoding)).split(' ')
).replace('\0', '').encode(encoding)
lines = []
for l in sys.stdin:
l = l.rstrip('\n')
lines.extend(cjkwrap(l, charWidth).split('\n'))
pixelHeight = (fontSize+1) * len(lines)
font = ImageFont.truetype(fontPath,fontSize)
img=Image.new("RGBA", (pixelWidth+4,pixelHeight),(245,245,245))
draw = ImageDraw.Draw(img)
for n,line in enumerate(lines):
draw.text((4, n*(fontSize+1)),line.decode('utf8'),(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("output.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment