Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Jul 19, 2025

ImageDraw.multiline_text currently takes 14 arguments, and ImageDraw.multiline_textbbox currently accepts 12 arguments.

#6201 would probably lead to that number increasing further, and there are some comments that wonder why calculating multiline text bounding box dimensions requires an ImageDraw instance.

I'm going to suggest adding a new ImageText class. I consider this to be a tidying of the API, rather than something motivated by a pressing need, so if this is accepted, maybe we don't need an immediate deprecation.

Before

from PIL import Image, ImageDraw im = Image.new(mode, size) d = ImageDraw.Draw(im) d.multiline_textlength(text, font, direction, features, language, embedded_color) d.multiline_textbbox(xy, text, font, anchor, spacing, align, direction, features, language, stroke_width, embedded_color) d.text(xy, text, fill, font, anchor, spacing, align, direction, features, language, stroke_width, stroke_fill, embedded_color)

After

from PIL import ImageText text = ImageText.ImageText(text, font, mode, spacing, direction, features, language) text.embed_color() text.stroke(stroke_width, stroke_fill) text.get_length() text.get_bbox(xy, anchor, align) im = Image.new(mode, size) d = ImageDraw.Draw(im) d.text(xy, text, fill, anchor=anchor, align=align)

New features like #6201 would be made exclusive to the new class, something like text.wrap(width, height, scaling, limit).

If nothing else, this will separate the text handling logic from the rest of the ImageDraw code.

@radarhere radarhere force-pushed the imagetext branch 11 times, most recently from 75217c5 to 1346bfe Compare July 21, 2025 07:10
@radarhere radarhere marked this pull request as ready for review July 21, 2025 07:14
@radarhere radarhere requested a review from nulano July 21, 2025 08:27
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks much easier to use.

Bikeshedding: we have ImageDraw.Draw, should ImageText.ImageText be ImageText.Text?

Let's add to the 12.0.0 release notes and pepper some .. seealso:: in ImageDraw, perhaps next to multiline_textlength, multiline_textbbox and text, any other relevant places.

ImageText.rst could also include the before/after example from this PR description.

@hugovk hugovk added this to the 12.0.0 milestone Oct 15, 2025
@hugovk hugovk added the automerge Automatically merge PRs that are ready label Oct 15, 2025
@hugovk hugovk changed the title Added ImageText Add ImageText Oct 15, 2025
@mergify mergify bot merged commit e7b72a3 into python-pillow:main Oct 15, 2025
51 checks passed
@radarhere radarhere deleted the imagetext branch October 15, 2025 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Automatically merge PRs that are ready Enhancement

2 participants