/PyLf

A lightweight Python library for simulating Chinese handwriting

Primary LanguagePythonBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

PyLf

API Reference | Release Notes | Contributing

PyLf是一个轻量级模仿中文手写的Python库。其通过在处理过程中大量引入随机性来模仿汉字书写。

Installation

由于PyLf的依赖项Pillow会与PIL发生冲突,因此如若您已安装PIL,请先手动卸载

pip uninstall PIL

此外如若您并未安装setuptools,请先手动安装

pip install setuptools

安装PyLf:

pip install pylf

Quickstart

from PIL import Image, ImageFont
from pylf import handwrite
from multiprocessing import freeze_support  # 非Windows用户可删除此行


def main():
    template = dict(
        background=Image.new(mode='RGB', size=(800, 1000), color='rgb(255, 255, 255)'),
        box=(100, 200, 800 - 100, 1000 - 200),  # 设置在背景图上的手写区域
        font=ImageFont.truetype("YOUR FONT PATH"),  # 填入您所使用字体文件的路径
        font_size=50,
    )
    text = "我能吞下玻璃而不伤身体。"
    images = handwrite(text, template)
    for image in images:
        image.show()


if __name__ == '__main__':
    freeze_support()  # 非Windows用户可删除此行
    main()
    

如以上代码所示,函数pylf.handwrite是整个PyLf库的核心。而模板template则是本库的一个重要概念。模板包含着在手写模仿过程中所需的背景、排版 设置、字体、随机性强度等参数。这些参数通常因背景图和用户书写习惯的不同而不同。有关pylf.handwritetemplate的更多信息请参阅 API Reference。一般情况下,在第一次使用某个背景时,您需要根据自己的手写特征创建特定的模板(往往需要经历不断的调 试)。

另外,请您在更新PyLf后及时参阅Release Notes,以了解新版本的变化,特别是在主版本更新的时候(其中往往蕴含着不后向兼容的改动)。

Examples

:以下某些图片中之所以缺少个别字,是因为所使用生成该图片的字体本身缺少这些字。

  • 《荷塘月色》

示例代码:article.py

  • 《从百草园到三味书屋》

示例代码:even_odd.py

More