日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

通过python让打印出来的字体看起来像手写

發布時間:2023/12/9 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过python让打印出来的字体看起来像手写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

老婆因工作原因要寫書寫大量文字,時間久了手累到癱瘓

某天在zhihu上看到一篇文章https://www.zhihu.com/question/20308770(如何讓打印出來的字體看起來像手寫的?)

其中一位Gsllchb介紹了python的Handright庫

https://github.com/Gsllchb/Handright

研究了一下感覺十分符合需求,生成的字跡,每個字都會有細微的差別,排好序后直接打印看起來是真的像手寫

在原作者的基礎上簡單修改了一下代碼,直接從word的docx文檔直接讀取內容

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), # 字體顏色,括號內為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=",。", # 防止特定字符因排版算法的自動換行而出現在行首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("{}.jpg".format(i)) # 生成的圖片生成在當前目錄下

?

總結

以上是生活随笔為你收集整理的通过python让打印出来的字体看起来像手写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。