Skip to main content

生成文字图片

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)
  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()

Project details


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.3.20030216.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

pyfont-0.0.3.20030216-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file pyfont-0.0.3.20030216.tar.gz.

File metadata

  • Download URL: pyfont-0.0.3.20030216.tar.gz
  • Upload date:
  • Size: 13.2 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

Hashes for pyfont-0.0.3.20030216.tar.gz
Algorithm Hash digest
SHA256 d6df81404fbdef88929f4527187b67d7a68f83e022a820b88bf7ac7104bec0a9
MD5 ae14f01cc53e519181c945d81264e508
BLAKE2b-256 594f39ad038fff5668a014a4ac699f4a491e3061327ef288eff20d948b37f500

See more details on using hashes here.

File details

Details for the file pyfont-0.0.3.20030216-py3-none-any.whl.

File metadata

  • Download URL: pyfont-0.0.3.20030216-py3-none-any.whl
  • Upload date:
  • Size: 14.4 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

Hashes for pyfont-0.0.3.20030216-py3-none-any.whl
Algorithm Hash digest
SHA256 749550a3505e3b89e64c42e2bc88db8af12542dc3cf0faba43c3652b8af788d8
MD5 b74f9447ede38d650559ae0de475c38f
BLAKE2b-256 25a1f7cad23f936dc3bac8d2c071cbe26c9dd9c17034fe168bbede2e671dcd85

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page