Skip to content

Commit

Permalink
Merge pull request #5588 from kmilos/patch-2
Browse files Browse the repository at this point in the history
Ensure TIFF RowsPerStrip is multiple of 8 for JPEG compression
  • Loading branch information
radarhere committed Jul 18, 2021
2 parents 696b82e + abb192c commit 7484bb0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Tests/test_file_libtiff.py
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -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
Expand Down

0 comments on commit 7484bb0

Please sign in to comment.