Skip to content

Commit

Permalink
Merge pull request #5750 from radarhere/zero
Browse files Browse the repository at this point in the history
Fixed Python errors when saving a (0, 0) TIFF image
  • Loading branch information
hugovk committed Oct 13, 2021
2 parents 88850e6 + 8de429e commit b34430b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Tests/test_file_libtiff.py
Expand Up @@ -986,3 +986,10 @@ def test_save_multistrip(self, compression, tmp_path):
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)
8 changes: 6 additions & 2 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -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:
Expand Down

0 comments on commit b34430b

Please sign in to comment.