mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
Merge pull request #6539 from radarhere/thumbnail
Do not call load() before draft() in Image.thumbnail
This commit is contained in:
commit
9b53e3b581
|
@ -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")
|
||||
|
|
|
@ -2471,29 +2471,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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user