Skip to content

Commit b8ec793

Browse files
committed
Fixed ZeroDivisionError in thumbnail
1 parent 4634eaf commit b8ec793

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Tests/test_image_thumbnail.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def test_aspect():
6363
assert im.size == (75, 23) # ratio is 3.260869565217
6464

6565

66+
def test_division_by_zero():
67+
im = Image.new("L", (200, 2))
68+
im.thumbnail((75, 75))
69+
assert im.size == (75, 1)
70+
71+
6672
def test_float():
6773
im = Image.new("L", (128, 128))
6874
im.thumbnail((99.9, 99.9))

src/PIL/Image.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,9 @@ def round_aspect(number, key):
22772277
if x / y >= aspect:
22782278
x = round_aspect(y * aspect, key=lambda n: abs(aspect - n / y))
22792279
else:
2280-
y = round_aspect(x / aspect, key=lambda n: abs(aspect - x / n))
2280+
y = round_aspect(
2281+
x / aspect, key=lambda n: 0 if n == 0 else abs(aspect - x / n)
2282+
)
22812283
size = (x, y)
22822284

22832285
box = None

0 commit comments

Comments
 (0)