From 741f281e6d9a383daec3fe1d96db3a955f0f0eaa Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 18 Sep 2026 23:32:22 +1000 Subject: [PATCH] When shrinking in wrap(), wrap text even if no scaling is required --- Tests/test_imagetext.py | 3 +++ src/PIL/ImageText.py | 22 ++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Tests/test_imagetext.py b/Tests/test_imagetext.py index 507d8240918..699c4cba544 100644 --- a/Tests/test_imagetext.py +++ b/Tests/test_imagetext.py @@ -191,11 +191,14 @@ def test_wrap_shrink() -> None: assert text.wrap(50, 50, "shrink") is None assert isinstance(text.font, ImageFont.FreeTypeFont) assert text.font.size == 10 + assert text.text == "Hello\nWorld!" + text = ImageText.Text("Hello World!") with pytest.raises(ValueError, match="Text could not be scaled"): text.wrap(50, 15, ("shrink", 9)) assert text.wrap(50, 15, "shrink") is None + assert isinstance(text.font, ImageFont.FreeTypeFont) assert text.font.size == 8 text = ImageText.Text("Hello World!") diff --git a/src/PIL/ImageText.py b/src/PIL/ImageText.py index 1374d2cb485..10297d3f282 100644 --- a/src/PIL/ImageText.py +++ b/src/PIL/ImageText.py @@ -219,18 +219,16 @@ def wrap( font = self.font wrap = _Wrap(self, width, height, font) if scaling == "shrink": - if not wrap.remaining_text: - return None - - size = math.ceil(font.size) - while wrap.remaining_text: - if size == max(limit, 1): - msg = "Text could not be scaled" - raise ValueError(msg) - size -= 1 - font = self.font.font_variant(size=size) - wrap = _Wrap(self, width, height, font) - self.font = font + if wrap.remaining_text: + size = math.ceil(font.size) + while wrap.remaining_text: + if size == max(limit, 1): + msg = "Text could not be scaled" + raise ValueError(msg) + size -= 1 + font = self.font.font_variant(size=size) + wrap = _Wrap(self, width, height, font) + self.font = font else: if wrap.remaining_text: msg = "Text could not be scaled"