From 01e423da00c7039e447a477c21a7414a2ccae5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Komar=C4=8Devi=C4=87?= <4973094+kmilos@users.noreply.github.com> Date: Wed, 7 Jul 2021 09:38:06 +0200 Subject: [PATCH 1/2] Ensure TIFF RowsPerStrip is multiple of 8 for JPEG compression --- src/PIL/TiffImagePlugin.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index a5e2bb53d..3f34cc9ac 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1577,6 +1577,9 @@ def _save(im, fp, filename): # aim for 64 KB strips when using libtiff writer if libtiff: rows_per_strip = 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 From abb192c9b39595839e4fcf227d43c80abf7032d1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 7 Jul 2021 19:16:44 +1000 Subject: [PATCH 2/2] Added test --- Tests/test_file_libtiff.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index e2f0df84a..8de4f0784 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -968,10 +968,11 @@ class TestFileLibTiff(LibTiffTestCase): assert str(e.value) == "-9" TiffImagePlugin.READ_LIBTIFF = False - def test_save_multistrip(self, tmp_path): + @pytest.mark.parametrize("compression", ("tiff_adobe_deflate", "jpeg")) + def test_save_multistrip(self, compression, tmp_path): im = hopper("RGB").resize((256, 256)) out = str(tmp_path / "temp.tif") - im.save(out, compression="tiff_adobe_deflate") + im.save(out, compression=compression) with Image.open(out) as im: # Assert that there are multiple strips