Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Apr 17, 2025

Resolves #8897

When justifying text,

  • do not justify the last line. Keep it left aligned
  • fix position of justified words when using middle or right anchoring
  • fixed justifying a single word. This isn't mentioned in the issue, but in the process of investigating it, I found that
from PIL import Image, ImageDraw
im = Image.new("L", (1, 1))
draw = ImageDraw.Draw(im)
draw.multiline_text((0, 0), "test\ntext", align="justify")

caused

Traceback (most recent call last):
  File "demo.py", line 4, in <module>
    draw.multiline_text((0, 0), "test\ntext", align="justify")
  File "PIL/ImageDraw.py", line 820, in multiline_text
    font, anchor, lines = self._prepare_multiline_text(
  File "PIL/ImageDraw.py", line 789, in _prepare_multiline_text
    left += word_widths[i] + width_difference / (len(words) - 1)
ZeroDivisionError: float division by zero

@radarhere radarhere changed the title Improved justifying text Improve justifying text Apr 19, 2025
@hugovk hugovk merged commit c96b277 into python-pillow:main Jun 27, 2025
55 checks passed
@radarhere radarhere deleted the justify branch June 27, 2025 15:17
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.

Text justify doesn't work with middle and right anchor (and last line is also always justified)

2 participants