Fix size calculation of Image.thumbnail()

This commit is contained in:
orlnub123 2020-02-04 20:14:00 +00:00 committed by Andrew Murray
parent 29fee8fc43
commit e9ef1d236d
2 changed files with 16 additions and 9 deletions

View File

@ -50,6 +50,14 @@ def test_aspect():
im.thumbnail((33, 33)) im.thumbnail((33, 33))
assert im.size == (21, 33) # ratio is 0.6363636364 assert im.size == (21, 33) # ratio is 0.6363636364
im = Image.new("L", (145, 100)) # ratio is 1.45
im.thumbnail((50, 50))
assert im.size ==(50, 34) # ratio is 1.47058823529
im = Image.new("L", (100, 145)) # ratio is 0.689655172414
im.thumbnail((50, 50))
assert im.size == (34, 50) # ratio is 0.68
def test_float(): def test_float():
im = Image.new("L", (128, 128)) im = Image.new("L", (128, 128))

View File

@ -2237,19 +2237,18 @@ class Image:
""" """
# preserve aspect ratio # preserve aspect ratio
x, y = self.size x, y = size
if x > size[0]: aspect = self.width / self.height
y = max(round(y * size[0] / x), 1) if x / y >= aspect:
x = round(size[0]) x = max(y * aspect, 1)
if y > size[1]: else:
x = max(round(x * size[1] / y), 1) y = max(x / aspect, 1)
y = round(size[1]) size = (round(x), round(y))
size = x, y
box = None
if size == self.size: if size == self.size:
return return
box = None
if reducing_gap is not None: if reducing_gap is not None:
res = self.draft(None, (size[0] * reducing_gap, size[1] * reducing_gap)) res = self.draft(None, (size[0] * reducing_gap, size[1] * reducing_gap))
if res is not None: if res is not None: