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