Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call load() before draft() in Image.thumbnail #6539

Merged
merged 1 commit into from Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -2473,29 +2473,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