#-*- coding:utf8 -*-
from PIL import Image, ImageDraw, ImageFont
import codecs as cs
def genFontImage(font, char):
size = font.size
image = Image.new('1', (size, size))
draw = ImageDraw.Draw(image)
draw.text((0, 0), char, font=font, fill='#ff0000')
return image
if __name__ == '__main__':
size = 14
font = ImageFont.truetype('simhei.ttf', size)
hansfile = cs.open('gbl1.txt', 'r', 'utf-8')
hans = hansfile.read()
hansfile.close()
for han in hans[:10]:
image = genFontImage(font,han)
image.save(str(hans.index(han))+'.png')
需要字体太小,可以先生成大图片然后根据临近点取值生成小字体图片。