Skip to content

Commit

Permalink
Merge pull request #6539 from radarhere/thumbnail
Browse files Browse the repository at this point in the history
Do not call load() before draft() in Image.thumbnail
  • Loading branch information
hugovk committed Sep 11, 2022
2 parents d843759 + 38b53a9 commit 9b53e3b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
22 changes: 22 additions & 0 deletions Tests/test_image_thumbnail.py
Expand Up @@ -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")
Expand Down
42 changes: 27 additions & 15 deletions src/PIL/Image.py
Expand Up @@ -2471,29 +2471,41 @@ def thumbnail(self, size, resample=Resampling.BICUBIC, reducing_gap=2.0):
: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)
Expand Down

0 comments on commit 9b53e3b

Please sign in to comment.