Skip to main content

Creates PNG from text using Pillow

Project description

piltext

codecov PyPi Version

Creates PNG from text using Pillow

Installation

PyPI

pip install piltext

or from source

git clone https://github.com/holgern/piltext.git
cd piltext
python3 setup.py install

License

MIT

Usage

Import the import parts

from piltext import FontManager, ImageDrawer, TextGrid

Download fonts:

f = FontManager(default_font_size=20)
font1 = f.download_google_font("ofl", "roboto", "Roboto[wdth,wght].ttf")
font2 = f.download_google_font("ofl", "jersey10", "Jersey10-Regular.ttf")
f.default_font_name = font1
print(f.list_available_fonts())

Fonts with variations can be used

print(f.get_variation_names())

Create image with texts:

image = ImageDrawer(100, 100, f)
xy = (0, 0)
w, h, font_size = image.draw_text("Test", xy, font_variation="Bold")
xy = (xy[0], xy[1] + h)
w, h, font_size = image.draw_text("Test", xy)
xy = (xy[0] + w, xy[1])
w, h, font_size = image.draw_text("Test", xy)
xy = (xy[0], xy[1] + h)
w, h, font_size = image.draw_text("Test", xy, font_name=font2, fill=128)
xy = (0, xy[1] + h)
w, h, font_size = image.draw_text(
    "Test", xy, end=(100, 100), font_variation="Condensed Medium"
)
print(f"w: {w}, h:{h}, font_size: {font_size}")

image.finalize(inverted=False)
display(image.get_image())

Anchor can be used:

image = ImageDrawer(480, 280, f)
xy = (5, 3)
w, h, font_size = image.draw_text(
    "Long Text 1", xy, end=(480, (280 - 15) / 3), anchor="lt"
)
xy = (5, int(h * 0.85))
w, h, font_size = image.draw_text(
    "Long Text 2", xy, end=(480, xy[1] * 1.5), anchor="lt"
)
xy = (5, xy[1] + h)
w, h, font_size = image.draw_text(
    "Long Text 3", xy, font_size=font_size, font_variation="Thin", anchor="lt"
)
xy = (5, xy[1] + h)
w, h, font_size = image.draw_text("Long Text 3", xy, font_size=font_size, anchor="lt")
w, h, font_size = image.draw_text(
    "Long Text 4", (480 - 5, 280 - 5), end=(5, 5), anchor="rs"
)

image.finalize(inverted=False)
display(image.get_image())

TextGrid

image = ImageDrawer(480, 280, f)

grid = TextGrid(7, 4, image, margin_x=2, margin_y=2)
grid.print_grid()
merge_list = [
    ((0, 0), (0, 3)),
    ((1, 0), (2, 1)),
    ((1, 2), (2, 3)),
    ((3, 0), (6, 3)),
]
grid.merge_bulk(merge_list)
grid.print_grid()

can be used to improve text layout:

image.initialize()
grid.set_text(0, "Test1", font_name=font2)
grid.set_text(1, "Test2")
grid.set_text(2, "Test3")
grid.set_text(3, "Test4", anchor="lt")
image.finalize(inverted=False)
display(image.get_image())

The text can also be set as bulk using a list:

image.initialize()
text_list = [
    {"start": 0, "text": "Test1", "font_name": font2},
    {"start": 1, "text": "Test2"},
    {"start": 2, "text": "Test3"},
    {"start": 3, "text": "Test4", "anchor": "lt", "fill": 128},
]
grid.set_text_list(text_list)
image.finalize(inverted=False)
display(image.get_image())

Pre-commit-config

Installation

$ pip install pre-commit

Using homebrew:

$ brew install pre-commit
$ pre-commit --version
pre-commit 2.10.0

Install the git hook scripts

$ pre-commit install

Run against all the files

pre-commit run --all-files
pre-commit run --show-diff-on-failure --color=always --all-files

Update package rev in pre-commit yaml

pre-commit autoupdate
pre-commit run --show-diff-on-failure --color=always --all-files

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

piltext-0.2.1.tar.gz (216.5 kB view details)

Uploaded Source

Built Distribution

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

piltext-0.2.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file piltext-0.2.1.tar.gz.

File metadata

  • Download URL: piltext-0.2.1.tar.gz
  • Upload date:
  • Size: 216.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for piltext-0.2.1.tar.gz
Algorithm Hash digest
SHA256 85b453abda63043fcc291aa5222a4fe384f6139f8d8559a2e1ff74d77d44bb63
MD5 e268deda3f041c35bae8ad10e18c9f13
BLAKE2b-256 9ae56844e2d76627d90700b84bb4b630722cfd27ccb5bb5c52138b9cdf1f643a

See more details on using hashes here.

File details

Details for the file piltext-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: piltext-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for piltext-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d4e8bc0948689a303732eb556e6def7abbc861d7368ed74e05b1d7ca54bb49cd
MD5 e171e6284503268f328ca2786d720fc6
BLAKE2b-256 8096ee1f0d7641d55c56662091c0f581dc37fb9ca45163a1ab9e73584b44fc91

See more details on using hashes here.

Supported by

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