# coding: utf-8
from PIL import Image, ImageFont
from handright import Template, handwrite
text = """
人间小苦瓜
"""
template = Template(
background=Image.new(mode="1", size=(900, 1000), color=1),
font=ImageFont.truetype("C:/Windows/Fonts/simfang.ttf", size=100),
line_spacing=150,
fill=0, # 字体“颜色”
left_margin=100,
top_margin=100,
right_margin=100,
bottom_margin=100,
word_spacing=15,
line_spacing_sigma=6, # 行间距随机扰动
font_size_sigma=20, # 字体大小随机扰动
word_spacing_sigma=3, # 字间距随机扰动
end_chars=",。", # 防止特定字符因排版算法的自动换行而出现在行首
perturb_x_sigma=4, # 笔画横向偏移随机扰动
perturb_y_sigma=4, # 笔画纵向偏移随机扰动
perturb_theta_sigma=0.05, # 笔画旋转偏移随机扰动
)
images = handwrite(text, template)
for i, im in enumerate(images):
assert isinstance(im, Image.Image)
im.show()
im.save("C:Anaconda3envspy3.7hr/{}.webp".format(i))
效果展示:
遇到的问题:
1.报错:OSError: cannot open resource
font=ImageFont.truetype("C:/Windows/Fonts/simfang.ttf", size=100)
字体路径错误
解决办法:cmd运行cd C:WindowsFonts,找到自己电脑上的路径然后copy
2.报错:ModuleNotFoundError: No module named 'handright'下载源码时,没有下载到我pycharm创建项目所在的文件夹
解决办法:应该把源码文件移到项目所在文件夹



