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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant