From b8ec793898c9cebd9f3b109d4083be09b17f8abd Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 15 May 2020 18:29:52 +1000 Subject: [PATCH] Fixed ZeroDivisionError in thumbnail --- Tests/test_image_thumbnail.py | 6 ++++++ src/PIL/Image.py | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Tests/test_image_thumbnail.py b/Tests/test_image_thumbnail.py index f4ed8e746..da63efe55 100644 --- a/Tests/test_image_thumbnail.py +++ b/Tests/test_image_thumbnail.py @@ -63,6 +63,12 @@ def test_aspect(): assert im.size == (75, 23) # ratio is 3.260869565217 +def test_division_by_zero(): + im = Image.new("L", (200, 2)) + im.thumbnail((75, 75)) + assert im.size == (75, 1) + + def test_float(): im = Image.new("L", (128, 128)) im.thumbnail((99.9, 99.9)) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0c8b42a09..8c5fff8ed 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2277,7 +2277,9 @@ class Image: 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: abs(aspect - x / n)) + y = round_aspect( + x / aspect, key=lambda n: 0 if n == 0 else abs(aspect - x / n) + ) size = (x, y) box = None