/PyLf

A lightweight Python library for simulating Chinese handwriting

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

PyLf

A lightweight Python library for simulating Chinese handwriting

version python implementation gitter license Build Status Code style: black

Tutorial | Examples | Release Notes | Contributing

Vision

Reveal the nature of Chinese handwriting and use it to implement beautiful, simple and easy-to-use interfaces.

Algorithm & Implementation

首先,在水平位置、竖直位置和字体大小三个自由度上,对每个字的整体做随机扰动。随后,在水平位置、竖直位置 和旋转角度三个自由度上,对每个字的每个笔画做随机扰动。

目前,PyLf基于Pillow开发,并在内部使用multiprocessing 做并行加速。

Installation

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

pip uninstall PIL

安装PyLf:

pip install pylf

Quick Start

from PIL import Image, ImageFont
from pylf import handwrite


if __name__ == '__main__':
    template = {
        "background": Image.new(mode="1", size=(2000, 2000), color="white"),
        "margin": {"left": 150, "right": 150, "top": 200, "bottom": 200},
        "line_spacing": 150,
        "font_size": 100,
        "font": ImageFont.truetype("path/to/my/font.ttf")
    }
    for image in handwrite("我能吞下玻璃而不伤身体。", template):
        image.show()

请参阅Tutorial

Features

特性 相关参数
设置背景 background
设置字体及其大小、颜色 font, font_size, color
设置页边距、行间距、字间距 margin, line_spacing, word_spacing
调节排版随机化的强弱 font_size_sigma, line_spacing_sigma, word_spacing_sigma
调节笔画随机扰动的强弱 perturb_x_sigma, perturb_y_sigma, perturb_theta_sigma
设置在行末不换行的字符集(如:'。', '》', ')', ']') is_end_char_fn
设置在排版时只占其宽度一半的字符集(如:',', '。', '!') is_half_char_fn
并行加速 worker
周期性模板 template2(详情:pylf.handwrite2)