python代码模拟 手写字体
生活随笔
收集整理的這篇文章主要介紹了
python代码模拟 手写字体
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# coding: utf-8
from PIL import Image, ImageFontfrom handright import Template, handwritetext = """
人間小苦瓜
"""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:\Anaconda3\envs\py3.7\hr/{}.webp".format(i))
效果展示:?
?遇到的問題:
1.報錯:OSError: cannot open resource
? font=ImageFont.truetype("C:/Windows/Fonts/simfang.ttf", size=100)字體路徑錯誤
解決辦法:cmd運行cd C:\Windows\Fonts,找到自己電腦上的路徑然后copy
2.報錯:ModuleNotFoundError: No module named 'handright'
下載源碼時,沒有下載到我pycharm創建項目所在的文件夾
解決辦法:應該把源碼文件移到項目所在文件夾
總結
以上是生活随笔為你收集整理的python代码模拟 手写字体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [css] 如何使用CSS的多列布局?
- 下一篇: [css] 列举CSS优化、提高性能的