目标:
生成指定字体的文字图片
具体代码
from PIL import Image, ImageDraw, ImageFont
width = 540
height = 360
# 字体所在的目录
font_path = 'C:\Windows\Font\LHANDW.TTF'
# 显示的文字
text = """1234567890
The quick brown fox jumped over a lazy dog.
Pack my box with five dozen liquor jugs.
The five boxing wizards jump quickly.
Quick wafting zephyrs vex bold Jim!
Waltz nymph, for quick jigs vex Bud?
"""
image = Image.new("RGB", (width, height), (255, 255, 255))
# 创建font对象
font = ImageFont.truetype(font_path, 20)
# 创建Draw对象
draw = ImageDraw.Draw(image)
# (5, 5) = (left, top) 即左边距和上边距
draw.text((5, 5), text, font=font, fill='#000000')
image.show()
image.save('test.png')