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.2.tar.gz (217.7 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.2-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: piltext-0.2.2.tar.gz
  • Upload date:
  • Size: 217.7 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.2.tar.gz
Algorithm Hash digest
SHA256 c17dd356577ed709e723a4db3b9ff267a3c6f45716e23b8c1664c5706730d87a
MD5 d2210eb81cc6b802e1a197d314759fb2
BLAKE2b-256 23c4cc972b96179d8f83b8775da3f179f4d717d5f0983fc1c60782223a628946

See more details on using hashes here.

File details

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

File metadata

  • Download URL: piltext-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 11.2 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6e91ff86c7cb007392abd5efabc847d0ac041546ee1d2f1eb21b39972984ed2a
MD5 e984248492adf258887d8b1efe1646cd
BLAKE2b-256 01ddc92f64bb869a92b0c56fbd268f54a82b5803d54c09b6fcde7bf81daa8155

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