python 生成字体图片


#-*- 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')
需要字体太小,可以先生成大图片然后根据临近点取值生成小字体图片。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM