imgrs

📝 Text API

Complete reference for rich text rendering operations on images.

Overview

imgrs provides comprehensive text rendering with support for:

Basic Text Methods

img.add_text(text, position, size=32, color=(0, 0, 0, 255), font_family="sans", font_weight="normal", font_style="normal", font_path=None, letter_spacing=0.0, opacity=1.0)

Draw basic text with enhanced font support.

Parameters:

Returns: Image

Examples:

# Basic text
img = img.add_text("Hello World", (50, 50), size=48)

# Colored text with custom font
img = img.add_text("Red Text", (50, 100), size=36, color=(255, 0, 0, 255))

# Using CSS color names
img = img.add_text("Blue Text", (50, 150), color="blue", size=32)

# Custom font file
img = img.add_text("Custom Font", (50, 200), font_path="/path/to/font.ttf")

# Semi-transparent text
img = img.add_text("Faded", (50, 250), opacity=0.7)

img.add_text_centered(text, y, size=32, color=(0, 0, 0, 255), font_family="sans", font_weight="normal", font_style="normal", font_path=None, opacity=1.0, letter_spacing=0.0)

Draw horizontally centered text.

Parameters:

Returns: Image

Example:

# Centered title
img = img.add_text_centered("Page Title", 50, size=48, font_weight="bold")

Advanced Text Methods

img.add_text_styled(text, position, size=32, color=(0, 0, 0, 255), font_family="sans", font_weight="normal", font_style="normal", font_path=None, letter_spacing=0.0, opacity=1.0, align="left", background=None, outline=None, shadow=None, glow=None, max_width=None, line_spacing=1.2, text_justify="left", rotation=0.0)

Draw text with comprehensive styling options (alias for add_text_advanced).

Parameters:

Returns: Image

Examples:

# Text with outline
img = img.add_text_styled(
    "OUTLINED",
    (100, 50),
    size=80,
    color=(255, 255, 255, 255),
    outline=(0, 0, 0, 255, 3.0)
)

# Text with shadow
img = img.add_text_styled(
    "Shadow Text",
    (100, 100),
    size=48,
    shadow=(3, 3, 100, 100, 100, 180)
)

# Text with glow effect
img = img.add_text_styled(
    "Glowing Text",
    (100, 150),
    size=48,
    color=(255, 255, 0, 255),
    glow=(255, 255, 0, 150, 5.0)
)

# Combined effects
img = img.add_text_styled(
    "EPIC TEXT",
    (200, 200),
    size=80,
    color=(255, 215, 0, 255),
    outline=(255, 140, 0, 255, 4.0),
    shadow=(5, 5, 0, 0, 0, 200),
    glow=(255, 215, 0, 100, 3.0),
    align="center"
)

img.add_text_advanced(text, position, size=32, color=(0, 0, 0, 255), font_family="sans", font_weight="normal", font_style="normal", font_path=None, letter_spacing=0.0, opacity=1.0, align="left", background=None, outline=None, shadow=None, glow=None, max_width=None, line_spacing=1.2, text_justify="left", rotation=0.0)

Full-featured text rendering with all styling options (same as add_text_styled).


img.add_text_multiline(text, position, size=32, color=(0, 0, 0, 255), font_family="sans", font_weight="normal", font_style="normal", font_path=None, line_spacing=1.2, letter_spacing=0.0, align="left", text_justify="left", max_width=None, opacity=1.0)

Draw multi-line text with enhanced formatting.

Parameters:

Returns: Image

Example:

multiline_text = """Line 1: First Line
Line 2: Second Line
Line 3: Third Line"""

img = img.add_text_multiline(
    multiline_text,
    (50, 50),
    size=32,
    line_spacing=1.5,
    align="center"
)

Font Management

Font Format Support

imgrs supports multiple font formats:

Font Families

Built-in font families:

Font Resolution

Fonts are resolved in this order:

  1. Explicit font_path if provided
  2. Font family matching in fonts directory
  3. System font fallbacks
  4. Embedded default font (DejaVu Sans)

Text Measurement

Image.get_text_size(text, size=32, font_family="sans", font_weight="normal", font_style="normal", font_path=None, letter_spacing=0.0)

Get text dimensions.

Parameters:

Returns: Tuple[int, int] - (width, height)

Example:

width, height = Image.get_text_size("Hello World", size=48, font_weight="bold")
print(f"Text will be {width}x{height} pixels")

Image.get_multiline_text_size(text, size=32, line_spacing=1.2, font_family="sans", font_weight="normal", font_style="normal", font_path=None, letter_spacing=0.0, max_width=None)

Get multiline text dimensions.

Parameters:

Returns: Tuple[int, int, int] - (width, height, line_count)


Image.get_text_box(text, x, y, size=32, font_family="sans", font_weight="normal", font_style="normal", font_path=None, letter_spacing=0.0)

Get complete text bounding box.

Returns: Dictionary with font metrics:


Font Discovery

Image.list_available_fonts()

List all available fonts in the system.

Returns: List[str] - List of font file paths

Example:

fonts = Image.list_available_fonts()
for font in fonts:
    print(font)

Color Formats

Text methods support multiple color formats:

# RGBA tuples (0-255)
red = (255, 0, 0, 255)
semi_transparent = (0, 255, 0, 128)

# CSS color names
img = img.add_text("Blue", (50, 50), color="blue")
img = img.add_text("Red", (50, 100), color="red")

# Hex colors (not yet supported)
# img = img.add_text("Green", (50, 150), color="#00FF00")

Advanced Examples

Meme Generator

def create_meme(image_path, top_text, bottom_text):
    img = Image.open(image_path)

    # Top text
    img = img.add_text_styled(
        top_text, (img.width // 2, 20), size=56,
        color=(255, 255, 255, 255),
        outline=(0, 0, 0, 255, 3.0),
        align="center"
    )

    # Bottom text
    img = img.add_text_styled(
        bottom_text, (img.width // 2, img.height - 70), size=56,
        color=(255, 255, 255, 255),
        outline=(0, 0, 0, 255, 3.0),
        align="center"
    )

    return img

Styled Quote

def create_quote(text, author):
    bg = Image.new("RGBA", (600, 400), (40, 44, 52, 255))

    # Main quote
    bg = bg.add_text_multiline(
        text, (300, 150), size=36,
        color=(230, 230, 230, 255),
        line_spacing=1.4, align="center"
    )

    # Author attribution
    bg = bg.add_text_styled(
        f"— {author}", (300, 320), size=24,
        color=(180, 180, 180, 255), align="center"
    )

    return bg

Glowing Title

def create_glowing_title(text, color=(255, 255, 0, 255)):
    canvas = Image.new("RGBA", (800, 200), (0, 0, 0, 255))

    canvas = canvas.add_text_styled(
        text, (400, 100), size=72,
        color=color,
        glow=(color[0], color[1], color[2], 150, 8.0),
        align="center"
    )

    return canvas

Performance Notes


See Also