diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 31b7abecd60..36d4b9b454f 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -128,6 +128,16 @@ def test_contain_round() -> None: assert new_im.height == 5 +def test_contain_zero() -> None: + im = Image.new("1", (1, 10)) + new_im = ImageOps.contain(im, (5, 5)) + assert new_im.width == 0 + + im = Image.new("1", (10, 1)) + new_im = ImageOps.contain(im, (5, 5)) + assert new_im.height == 0 + + @pytest.mark.parametrize( "image_name, expected_size", ( diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index f0ae142b9ba..c29b2abd950 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -297,6 +297,8 @@ def contain( new_width = round(image.width / image.height * size[1]) if new_width != size[0]: size = (new_width, size[1]) + if min(size) == 0: + return Image.new(image.mode, size) return image.resize(size, resample=method)