目标:
生成指定字体的文字图片
具体代码
from PIL import Image, ImageDraw, ImageFontwidth = 540height = 360# 字体所在的目录font_path = 'C:\Windows\Font\LHANDW.TTF'# 显示的文字text = """1234567890The 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')
如果觉得《将文字转换成图片》对你有帮助,请点赞、收藏,并留下你的观点哦!