通过python让打印出来的字体看起来像手写
生活随笔
收集整理的這篇文章主要介紹了
通过python让打印出来的字体看起来像手写
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
老婆因工作原因要寫書寫大量文字,時間久了手累到癱瘓
某天在zhihu上看到一篇文章https://www.zhihu.com/question/20308770(如何讓打印出來的字體看起來像手寫的?)
其中一位Gsllchb介紹了python的Handright庫
https://github.com/Gsllchb/Handright
研究了一下感覺十分符合需求,生成的字跡,每個字都會有細微的差別,排好序后直接打印看起來是真的像手寫
在原作者的基礎上簡單修改了一下代碼,直接從word的docx文檔直接讀取內(nèi)容
from PIL import Image, ImageFontfrom handright import Template, handwriteimport docx# 自動縮進排版,如果已在word里設置縮進可以注釋本段 # indent_size控制縮進,file_path文檔路徑 def get_text(file_path, indent_size=4): doc = docx.Document(file_path)texts = []indent = ''for i in range(0, indent_size):indent = indent + ' 'for paragraph in doc.paragraphs:texts.append(indent + paragraph.text)return '\n'.join(texts)# 根目錄下的word文檔 text = get_text('1.docx') template = Template(background=Image.open('016.jpg'), # 自定義背景圖片font_size=100,font=ImageFont.truetype("path/to/my/font.ttf"), # 字體選擇手寫體line_spacing=150,fill=(0, 0, 0), # 字體顏色,括號內(nèi)為RGB的值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=",。", # 防止特定字符因排版算法的自動換行而出現(xiàn)在行首perturb_x_sigma=4, # 筆畫橫向偏移隨機擾動perturb_y_sigma=4, # 筆畫縱向偏移隨機擾動perturb_theta_sigma=0.05, # 筆畫旋轉(zhuǎn)偏移隨機擾動 ) images = handwrite(text, template) for i, im in enumerate(images):assert isinstance(im, Image.Image)im.show()im.save("{}.jpg".format(i)) # 生成的圖片生成在當前目錄下?
總結(jié)
以上是生活随笔為你收集整理的通过python让打印出来的字体看起来像手写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse adt bundle不显
- 下一篇: 前端学习(2887):如何短时间内实现v