Pillow text alignment helper
Project description
Palign
Palign is a Python package that helps to render and align text in Pillow.
Full documentation is online at palign.dev.
Examples
draw_text
draws text of a given Style
at a set of coordinates:
from pathlib import Path
from PIL import Image, ImageDraw
from PIL.ImageFont import truetype
from palign import Style, draw_text
image = Image.new("RGB", (150, 28))
draw = ImageDraw.Draw(image)
style = Style(font=truetype("tests/font/ChelseaMarket-Regular.ttf", 21))
draw_text("Hello world!", draw, style, (0, 0))
image.save(Path() / "docs" / "images" / "example-0.png", "png")
Style
can also describe colour and tracking:
from pathlib import Path
from PIL import Image, ImageDraw
from PIL.ImageFont import truetype
from palign import Style, draw_text
image = Image.new("RGB", (260, 120), (255, 255, 255))
draw = ImageDraw.Draw(image)
style = Style(
color=(0, 0, 0),
font=truetype("tests/font/ChelseaMarket-Regular.ttf", 21),
)
draw_text("Red!", draw, style + Style(color=(255, 0, 0)), (0, 0))
draw_text("Increased tracking!", draw, style + Style(tracking=2), (0, 40))
draw_text("Decreased tracking!", draw, style + Style(tracking=-2), (0, 80))
image.save(Path() / "docs" / "images" / "example-1.png", "png")
If you specify a region to render within (rather than just a point to render at) then text can aligned and the background can be coloured:
from pathlib import Path
from PIL import Image, ImageDraw
from PIL.ImageFont import truetype
from bounden import Percent
from palign import Horizontal, Region, Style, Vertical, draw_text
image_region = Region.image(300, 720)
image = Image.new("RGB", image_region.size, (255, 255, 255))
draw = ImageDraw.Draw(image)
# Build a region to render the first block of text into.
#
# Start by contracting the image boundary to create a margin, then create a
# subregion that starts in the top corner, takes all available width (using
# Percent from the Bounden package) and is 70 pixels tall.
text_region = image_region.expand(-10).pregion(0, 0, Percent(100), 70)
style = Style(
border_color=(200, 200, 200),
border_radius=3,
border_width=1,
color=(0, 0, 0),
font=truetype("tests/font/ChelseaMarket-Regular.ttf", 21),
)
for vertical in Vertical:
for horizontal in Horizontal:
alignment = Style(horizontal=horizontal, vertical=vertical)
text = f"{vertical.name} {horizontal.name}"
draw_text(text, draw, style + alignment, text_region)
# Translate the region down by (text_region.height + 10) pixels for
# the next block.
text_region += (0, text_region.height + 10)
image.save(Path() / "docs" / "images" / "example-2.png", "png")
For detailed usage information, see the Style
class.
Installation
Palign requires Python 3.9 or later.
pip install palign
Support
Please raise bugs, request new features and ask questions at github.com/cariad/palign/issues.
Contributions
See CONTRIBUTING.md for contribution guidelines.
The Project
Palign is © 2022 Cariad Eccleston and released under the MIT License at github.com/cariad/palign.
Chelsea Market is copyright © 2011 Font Diner and redistributed under the SIL Open Font License 1.1.
The Author
Hello! 👋 I'm Cariad Eccleston and I'm a freelance backend and infrastructure engineer in the United Kingdom. You can find me at cariad.earth, github.com/cariad, linkedin.com/in/cariad and on Mastodon at @cariad@tech.lgbt.
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.