Skip to content

Commit e04f2ed

Browse files
authored
Merge pull request #5744 from kmilos/patch-2
2 parents b34430b + dbb0a41 commit e04f2ed

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Tests/test_file_libtiff.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,20 @@ def test_save_multistrip(self, compression, tmp_path):
987987
# Assert that there are multiple strips
988988
assert len(im.tag_v2[STRIPOFFSETS]) > 1
989989

990+
def test_save_single_strip(self, tmp_path):
991+
im = hopper("RGB").resize((256, 256))
992+
out = str(tmp_path / "temp.tif")
993+
994+
TiffImagePlugin.STRIP_SIZE = 2 ** 18
995+
try:
996+
997+
im.save(out, compression="tiff_adobe_deflate")
998+
999+
with Image.open(out) as im:
1000+
assert len(im.tag_v2[STRIPOFFSETS]) == 1
1001+
finally:
1002+
TiffImagePlugin.STRIP_SIZE = 65536
1003+
9901004
@pytest.mark.parametrize("compression", ("tiff_adobe_deflate", None))
9911005
def test_save_zero(self, compression, tmp_path):
9921006
im = Image.new("RGB", (0, 0))

src/PIL/TiffImagePlugin.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
READ_LIBTIFF = False
5959
WRITE_LIBTIFF = False
6060
IFD_LEGACY_API = True
61+
STRIP_SIZE = 65536
6162

6263
II = b"II" # little-endian (Intel style)
6364
MM = b"MM" # big-endian (Motorola style)
@@ -1617,11 +1618,9 @@ def _save(im, fp, filename):
16171618
ifd[COLORMAP] = tuple(v * 256 for v in lut)
16181619
# data orientation
16191620
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
1620-
# aim for 64 KB strips when using libtiff writer
1621+
# aim for given strip size (64 KB by default) when using libtiff writer
16211622
if libtiff:
1622-
rows_per_strip = (
1623-
1 if stride == 0 else min((2 ** 16 + stride - 1) // stride, im.size[1])
1624-
)
1623+
rows_per_strip = 1 if stride == 0 else min(STRIP_SIZE // stride, im.size[1])
16251624
# JPEG encoder expects multiple of 8 rows
16261625
if compression == "jpeg":
16271626
rows_per_strip = min(((rows_per_strip + 7) // 8) * 8, im.size[1])

0 commit comments

Comments
 (0)