From 8de429ecb9ca4e18df237ea4071a9066ff3d147d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 8 Oct 2021 13:12:21 +1100 Subject: [PATCH] Fixed Python errors when saving a (0, 0) TIFF image --- Tests/test_file_libtiff.py | 7 +++++++ src/PIL/TiffImagePlugin.py | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 1d0c93f06..fc1831d99 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -986,3 +986,10 @@ class TestFileLibTiff(LibTiffTestCase): with Image.open(out) as im: # Assert that there are multiple strips assert len(im.tag_v2[STRIPOFFSETS]) > 1 + + @pytest.mark.parametrize("compression", ("tiff_adobe_deflate", None)) + def test_save_zero(self, compression, tmp_path): + im = Image.new("RGB", (0, 0)) + out = str(tmp_path / "temp.tif") + with pytest.raises(SystemError): + im.save(out, compression=compression) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index eb33e3218..baaa709d8 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1619,13 +1619,17 @@ def _save(im, fp, filename): stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8) # aim for 64 KB strips when using libtiff writer if libtiff: - rows_per_strip = min((2 ** 16 + stride - 1) // stride, im.size[1]) + rows_per_strip = ( + 1 if stride == 0 else min((2 ** 16 + stride - 1) // stride, im.size[1]) + ) # JPEG encoder expects multiple of 8 rows if compression == "jpeg": rows_per_strip = min(((rows_per_strip + 7) // 8) * 8, im.size[1]) else: rows_per_strip = im.size[1] - strip_byte_counts = stride * rows_per_strip + if rows_per_strip == 0: + rows_per_strip = 1 + strip_byte_counts = 1 if stride == 0 else stride * rows_per_strip strips_per_image = (im.size[1] + rows_per_strip - 1) // rows_per_strip ifd[ROWSPERSTRIP] = rows_per_strip if strip_byte_counts >= 2 ** 16: