From 38b53a9fd704570fb29abd10910ea7939b1185e1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 26 Aug 2022 20:33:51 +1000 Subject: [PATCH] Do not call load() before draft() --- Tests/test_image_thumbnail.py | 22 ++++++++++++++++++ src/PIL/Image.py | 42 ++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/Tests/test_image_thumbnail.py b/Tests/test_image_thumbnail.py index 20cc101ed..4fd07a2b4 100644 --- a/Tests/test_image_thumbnail.py +++ b/Tests/test_image_thumbnail.py @@ -97,6 +97,28 @@ def test_load_first(): im.thumbnail((64, 64)) assert im.size == (64, 10) + # Test thumbnail(), without draft(), + # on an image that is large enough once load() has changed the size + with Image.open("Tests/images/g4_orientation_5.tif") as im: + im.thumbnail((590, 88), reducing_gap=None) + assert im.size == (590, 88) + + +def test_load_first_unless_jpeg(): + # Test that thumbnail() still uses draft() for JPEG + with Image.open("Tests/images/hopper.jpg") as im: + draft = im.draft + + def im_draft(mode, size): + result = draft(mode, size) + assert result is not None + + return result + + im.draft = im_draft + + im.thumbnail((64, 64)) + # valgrind test is failing with memory allocated in libjpeg @pytest.mark.valgrind_known_error(reason="Known Failing") diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 4eb2dead6..afe1feede 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2473,29 +2473,41 @@ class Image: :returns: None """ - self.load() - x, y = map(math.floor, size) - if x >= self.width and y >= self.height: - return + provided_size = tuple(map(math.floor, size)) - def round_aspect(number, key): - return max(min(math.floor(number), math.ceil(number), key=key), 1) + def preserve_aspect_ratio(): + def round_aspect(number, key): + return max(min(math.floor(number), math.ceil(number), key=key), 1) - # preserve aspect ratio - aspect = self.width / self.height - if x / y >= aspect: - x = round_aspect(y * aspect, key=lambda n: abs(aspect - n / y)) - else: - y = round_aspect( - x / aspect, key=lambda n: 0 if n == 0 else abs(aspect - x / n) - ) - size = (x, y) + x, y = provided_size + if x >= self.width and y >= self.height: + return + + aspect = self.width / self.height + if x / y >= aspect: + x = round_aspect(y * aspect, key=lambda n: abs(aspect - n / y)) + else: + y = round_aspect( + x / aspect, key=lambda n: 0 if n == 0 else abs(aspect - x / n) + ) + return x, y box = None if reducing_gap is not None: + size = preserve_aspect_ratio() + if size is None: + return + res = self.draft(None, (size[0] * reducing_gap, size[1] * reducing_gap)) if res is not None: box = res[1] + if box is None: + self.load() + + # load() may have changed the size of the image + size = preserve_aspect_ratio() + if size is None: + return if self.size != size: im = self.resize(size, resample, box=box, reducing_gap=reducing_gap)