Skip to main content

Python bindings for imagetext

Project description

hero banner

A blazing fast text drawing library

imagetext-py is python bindings for imagetext

CI pypi Downloads PythonVersions discord


if you found this library useful, consider leaving a star ⭐

About

imagetext makes use of rusttype for font parsing and tiny-skia for drawing. It has a simple API that allows you to draw text with ease.

Currently imagetext-py does beat out Pillow for most of the cases I've tested, but I will setup some benchmarks soon.

Features

  • Multi-line text
  • Text wrapping
  • Text alignment
  • Font fallbacks
  • Text stroke
  • Gradient fills
  • Emojis! (almost every platform supported, including discord)
  • Global Font Database with css-like font querying

Note: emojis are fetched and cached from the internet during runtime, so you will need an internet connection to use them. The ability to use local emoji images will be added soon.

Installation

pip install imagetext-py

Example Usage

from PIL import Image
from imagetext_py import *

# supports fonts with fallbacks
FontDB.LoadFromDir(".")
font = FontDB.Query("coolvetica japanese")

# create a canvas to draw on
cv = Canvas(512, 512, (255, 255, 255, 255))

# paints are used to fill and stroke text
black = Paint.Color((0, 0, 0, 255))
rainbow = Paint.Rainbow((0.0,0.0), (256.0,256.0))

# if a font doesn't have a glyph for a character, it will use the fallbacks
text = "hello my 😓 n🐢ame i☕s 会のすべ aての構成員 nathan and i drink soup boop coop, the quick brown fox jumps over the lazy dog"

draw_text_wrapped(canvas=cv,              # the canvas to draw on
                  text=text, 
                  x=256, y=256,           # the position of the text
                  ax=0.5, ay=0.5,         # the anchor of the text
                  size=67,                # the size of the text
                  width=500,              # the width of the text
                  font=font,              
                  fill=black,
                  align=TextAlign.Center,
                  stroke=2.0,             # the stroke width (optional)
                  stroke_color=rainbow,
                  draw_emojis=True)   # the stroke color (optional)

# you can convert the canvas to a PIL image
im: Image.Image = cv.to_image()
im.save("test.png")

# or you can just get the raw bytes
dimensions, bytes = cv.to_bytes()

# you can also save directly to a file
cv.save("test.png")

produces this image:

test.png

Pillow and FontDB Usage

from PIL import Image
from imagetext_py import *

FontDB.SetDefaultEmojiOptions(EmojiOptions(parse_discord_emojis=True))
FontDB.LoadFromDir(".")

font = FontDB.Query("coolvetica japanese")

with Image.new("RGBA", (512, 512), "white") as im:
    with Writer(im) as w:
        w.draw_text_wrapped(
            text="hello from python 😓 lol, <:blobpain:739614945045643447> " \
                 "ほまみ <:chad:682819256173461522><:bigbrain:744344773229543495> " \
                 "emojis workin",
            x=256, y=256,
            ax=0.5, ay=0.5,
            width=500,
            size=90,
            font=font,
            fill=Paint.Color((0, 0, 0, 255)),
            align=TextAlign.Center,
            stroke=2.0,
            stroke_color=Paint.Rainbow((0.0,0.0), (256.0,256.0)),
            draw_emojis=True
        )
    im.save("test.png")

produces this image:

test.png

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

imagetext_py-2.2.0-cp38-abi3-win_arm64.whl (3.6 MB view hashes)

Uploaded CPython 3.8+ Windows ARM64

imagetext_py-2.2.0-cp38-abi3-win_amd64.whl (3.7 MB view hashes)

Uploaded CPython 3.8+ Windows x86-64

imagetext_py-2.2.0-cp38-abi3-win32.whl (3.6 MB view hashes)

Uploaded CPython 3.8+ Windows x86

imagetext_py-2.2.0-cp38-abi3-musllinux_1_2_x86_64.whl (7.5 MB view hashes)

Uploaded CPython 3.8+ musllinux: musl 1.2+ x86-64

imagetext_py-2.2.0-cp38-abi3-musllinux_1_2_i686.whl (7.4 MB view hashes)

Uploaded CPython 3.8+ musllinux: musl 1.2+ i686

imagetext_py-2.2.0-cp38-abi3-musllinux_1_2_armv7l.whl (7.2 MB view hashes)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARMv7l

imagetext_py-2.2.0-cp38-abi3-musllinux_1_2_aarch64.whl (7.9 MB view hashes)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

imagetext_py-2.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ x86-64

imagetext_py-2.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (7.7 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ s390x

imagetext_py-2.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (7.9 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64le

imagetext_py-2.2.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (8.2 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64

imagetext_py-2.2.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (7.5 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ i686

imagetext_py-2.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (6.9 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

imagetext_py-2.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.6 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

imagetext_py-2.2.0-cp38-abi3-macosx_10_12_x86_64.whl (4.1 MB view hashes)

Uploaded CPython 3.8+ macOS 10.12+ x86-64

imagetext_py-2.2.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (8.1 MB view hashes)

Uploaded CPython 3.8+ macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

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