生成文字图片
Project description
Documentation
生成文字图片:在一定的区域,支持换行,丢弃,缩小来完成适应区域 The documentation is hosted at https://github.com/BingerYang/pyfont
Installation
pip install pyfont
Usage
example: 1. write 通过控制 limit_width, limit_count 与 传入回调返回,决定是否保留换行,或者截断丢弃
# -*- coding: utf-8 -*- from pyfont import FontAttr, FontDraw from PIL import Image image = Image.new('RGBA', (int(400), int(400)), (1, 1, 1, 0)) path = 'C:\Windows\Fonts\simsun.ttc' font = FontAttr(path=path, size=20, limit_width=220, fill_color=(1, 1, 1, 255)) # obj = FontDraw(bg=image, font=font) obj = FontDraw(font=font) # 通过控制 limit_width,limit_count 与 传入回调返回,决定是否换行,或者丢弃 # limit_text_cb=None 不处理(超过) # limit_text_cb 返回值False丢弃多余,返回True(保留换行) def limit_text_cb(index, line, state): print('index:', index, line[index:], state) return True result = obj.write(text="我们是中国人,我爱我的祖国\n你好", limit_text_cb=limit_text_cb) img = result.img print(img.size) img.show() print(font.size, font.line_height)
write_by_change_size 通过 limit_width, limit_height 适当的缩小字体范围,以适应区域
from pyfont import FontAttr, FontDraw from PIL import Image image = Image.new('RGBA', (int(400), int(400)), (1, 1, 1, 0)) path = 'C:\Windows\Fonts\simsun.ttc' font = FontAttr(path=path, size=20, limit_width=220, fill_color=(1, 1, 1, 255)) # obj = FontDraw(bg=image, font=font) obj = FontDraw(font=font) result = obj.write_by_change_size(text="我们是中国人,我爱我的祖国\n你好") print(result.offset, result.lines) img = result.img print(img.size) img.show()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyfont-0.0.2.2002419.tar.gz
(11.6 kB
view details)
Built Distribution
File details
Details for the file pyfont-0.0.2.2002419.tar.gz
.
File metadata
- Download URL: pyfont-0.0.2.2002419.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9adb995448c39ea18c8f47a0d171f43cb15bd6069f93765f9be25a7c0e106d6b |
|
MD5 | e477f084ef013ef02231e2c260fc2222 |
|
BLAKE2b-256 | cf4cea3c12eeec73e586a4f4e3b86efa97b3ca5f221ca9d1febb0b75559a6bd7 |
File details
Details for the file pyfont-0.0.2.2002419-py3-none-any.whl
.
File metadata
- Download URL: pyfont-0.0.2.2002419-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 724b0f7172df17dab659fb19bd9324bcd60e21e9dd0fd3b2c99f97110ffed8e7 |
|
MD5 | f52d65c9398a6d8399fcec8f4377d72d |
|
BLAKE2b-256 | edc812b9bafc957f3132156703e67ee52ff76350321c773eea6410d09004c9d5 |