Skip to main content

pyfont

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)
  1. 通过 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)

text="我们是中国人,我爱我的祖国\n你好"
size = obj.get_size_at_limit_range(text, font.size)
font.size = size
result = obj.write(text=text)
print(result.offset, result.lines)
img = result.img
print(img.size)
img.show()

Metadata

Release files for pyfont 0.0.3.20041618

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfont 0.0.3.20041618
File Size Uploaded Provenance
pyfont-0.0.3.20041618.tar.gz 13.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pyfont 0.0.3.20041618
File Interpreter ABI Platform
pyfont-0.0.3.20041618-py3-none-any.whl Python 3 none any Details

Total release size: 27.2 kB

Release files / pyfont-0.0.3.20041618.tar.gz

Download URL pyfont-0.0.3.20041618.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
cd5eef40c5b0cd0a6491fdbc4dce9b6f8db35f72069e72c1db65d5aa04812287
BLAKE2b-256 checksum
How to use checksums
de2c76206a615bc64270b5e1897e2a1ed6b2ed196da9d66f2b3a90662fc65184
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4

Release files / pyfont-0.0.3.20041618-py3-none-any.whl

Download URL pyfont-0.0.3.20041618-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d782095fc9dc314cfc467ff8bf124ad66058970b2a9474b1c6b09e0791471171
BLAKE2b-256 checksum
How to use checksums
70e5ac16e4dbd08767a6e47588b09a30d405e7e0ed21f5c86ee6e21704eeac23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4

Security for pyfont 0.0.3.20041618

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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