python代码模拟 手写字体
生活随笔
收集整理的這篇文章主要介紹了
python代码模拟 手写字体
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
# 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, # 行間距隨機(jī)擾動(dòng)font_size_sigma=20, # 字體大小隨機(jī)擾動(dòng)word_spacing_sigma=3, # 字間距隨機(jī)擾動(dòng)end_chars=",。", # 防止特定字符因排版算法的自動(dòng)換行而出現(xiàn)在行首perturb_x_sigma=4, # 筆畫橫向偏移隨機(jī)擾動(dòng)perturb_y_sigma=4, # 筆畫縱向偏移隨機(jī)擾動(dòng)perturb_theta_sigma=0.05, # 筆畫旋轉(zhuǎn)偏移隨機(jī)擾動(dòng)
)
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.報(bào)錯(cuò):OSError: cannot open resource
? font=ImageFont.truetype("C:/Windows/Fonts/simfang.ttf", size=100)字體路徑錯(cuò)誤
解決辦法:cmd運(yùn)行cd C:\Windows\Fonts,找到自己電腦上的路徑然后copy
2.報(bào)錯(cuò):ModuleNotFoundError: No module named 'handright'
下載源碼時(shí),沒有下載到我pycharm創(chuàng)建項(xiàng)目所在的文件夾
解決辦法:應(yīng)該把源碼文件移到項(xiàng)目所在文件夾
總結(jié)
以上是生活随笔為你收集整理的python代码模拟 手写字体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [css] 如何使用CSS的多列布局?
- 下一篇: 基于python生成手写的笔记