Skip to content

Commit 861a031

Browse files
authored
Merge pull request #5514 from kmilos/fix_tiff_rowsperstrip
2 parents f668368 + 100299a commit 861a031

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Tests/test_file_libtiff.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
from PIL import Image, ImageFilter, TiffImagePlugin, TiffTags, features
12-
from PIL.TiffImagePlugin import SUBIFD
12+
from PIL.TiffImagePlugin import STRIPOFFSETS, SUBIFD
1313

1414
from .helper import (
1515
assert_image_equal,
@@ -967,3 +967,12 @@ def test_realloc_overflow(self):
967967
# Assert that the error code is IMAGING_CODEC_MEMORY
968968
assert str(e.value) == "-9"
969969
TiffImagePlugin.READ_LIBTIFF = False
970+
971+
def test_save_multistrip(self, tmp_path):
972+
im = hopper("RGB").resize((256, 256))
973+
out = str(tmp_path / "temp.tif")
974+
im.save(out, compression="tiff_adobe_deflate")
975+
976+
with Image.open(out) as im:
977+
# Assert that there are multiple strips
978+
assert len(im.tag_v2[STRIPOFFSETS]) > 1

src/PIL/TiffImagePlugin.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1574,12 +1574,22 @@ def _save(im, fp, filename):
15741574
ifd[COLORMAP] = tuple(v * 256 for v in lut)
15751575
# data orientation
15761576
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
1577-
ifd[ROWSPERSTRIP] = im.size[1]
1578-
strip_byte_counts = stride * im.size[1]
1577+
# aim for 64 KB strips when using libtiff writer
1578+
if libtiff:
1579+
rows_per_strip = min((2 ** 16 + stride - 1) // stride, im.size[1])
1580+
else:
1581+
rows_per_strip = im.size[1]
1582+
strip_byte_counts = stride * rows_per_strip
1583+
strips_per_image = (im.size[1] + rows_per_strip - 1) // rows_per_strip
1584+
ifd[ROWSPERSTRIP] = rows_per_strip
15791585
if strip_byte_counts >= 2 ** 16:
15801586
ifd.tagtype[STRIPBYTECOUNTS] = TiffTags.LONG
1581-
ifd[STRIPBYTECOUNTS] = strip_byte_counts
1582-
ifd[STRIPOFFSETS] = 0 # this is adjusted by IFD writer
1587+
ifd[STRIPBYTECOUNTS] = (strip_byte_counts,) * (strips_per_image - 1) + (
1588+
stride * im.size[1] - strip_byte_counts * (strips_per_image - 1),
1589+
)
1590+
ifd[STRIPOFFSETS] = tuple(
1591+
range(0, strip_byte_counts * strips_per_image, strip_byte_counts)
1592+
) # this is adjusted by IFD writer
15831593
# no compression by default:
15841594
ifd[COMPRESSION] = COMPRESSION_INFO_REV.get(compression, 1)
15851595

0 commit comments

Comments
 (0)