Skip to main content

PyLf

A lightweight Python library for simulating Chinese handwriting

version python implementation gitter license Build Status

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

Quickstart

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


def main():
    template = {"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()

首先,您可通过使用pydoc来查看PyLf的完整API文档。

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

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

Features

特性 相关参数
设置背景 background
设置字体及其大小、颜色 font, font_size, color
设置手写区域、行间距、字间距 box, line_spacing, word_spacing
调节排版随机化的强弱 font_size_sigma, line_spacing_sigma, word_spacing_sigma
调节页面随机扰动的强弱 alpha
设置在行末不换行的字符集(如:'。', '》', ')', ']') is_end_char
设置在排版时只占其宽度一半的字符集(如:',', '。', '!') is_half_char
抗锯齿 anti_aliasing
多线程加速 worker
周期性模板 template2(详情:pylf.handwrite2)

Examples

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

  • 《荷塘月色》

示例代码:article.py

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

示例代码:even_odd.py

欲查看更多示例请前往PyLf-examples

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylf-1.4.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylf-1.4.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file pylf-1.4.0.tar.gz.

File metadata

  • Download URL: pylf-1.4.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6rc1

File hashes

Hashes for pylf-1.4.0.tar.gz
Algorithm Hash digest
SHA256 0da154051d0b92d1a390e38f34efeab11d096b08a5935f0d0cab91b85a896492
MD5 212a583746ccb372f2ce8afa905333e9
BLAKE2b-256 2736140a13682749d139c8dece51fc5ebdf16780ac42f9907dafb47aea3e1b48

See more details on using hashes here.

File details

Details for the file pylf-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: pylf-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6rc1

File hashes

Hashes for pylf-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d719e884794d2eb3d273073f51adc4e68bdc0c21528c36310a7a738eba108761
MD5 f6ab120ca1c860de35e30336067b8f3c
BLAKE2b-256 f080f467dca864a0a779daf396908531f1a62657fdefce7fc2aaf1d2a5ca79c0

See more details on using hashes here.

Release history Release notifications | RSS feed

4.1.0

2 files

4.0.0

2 files

3.4.0

2 files

3.3.2

2 files

3.3.1

2 files

3.3.0

2 files

3.2.3

2 files

3.2.2

2 files

3.2.1

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

2 files

2.1.0

2 files

2.0.0

2 files

This release

1.4.0 This release

2 files

1.3.0

2 files

1.2.0

2 files

1.1.4

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.0

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page