diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index e2f0df84a8c..8de4f0784f2 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -968,10 +968,11 @@ def test_realloc_overflow(self): 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 diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index a5e2bb53d17..3f34cc9acac 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