Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Improve justifying text

Open radarhere opened this issue 8 months ago • 0 comments

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 avatar Apr 17 '25 10:04 radarhere