Skip to content

Commit 2b14d83

Browse files
committed
Added strip_size as TIFF encoder argument
1 parent f5b27f9 commit 2b14d83

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

Tests/test_file_libtiff.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,14 +1011,18 @@ def test_save_multistrip(self, compression, tmp_path):
10111011
# Assert that there are multiple strips
10121012
assert len(im.tag_v2[STRIPOFFSETS]) > 1
10131013

1014-
def test_save_single_strip(self, tmp_path):
1014+
@pytest.mark.parametrize("argument", (True, False))
1015+
def test_save_single_strip(self, argument, tmp_path):
10151016
im = hopper("RGB").resize((256, 256))
10161017
out = str(tmp_path / "temp.tif")
10171018

1018-
TiffImagePlugin.STRIP_SIZE = 2**18
1019+
if not argument:
1020+
TiffImagePlugin.STRIP_SIZE = 2**18
10191021
try:
1020-
1021-
im.save(out, compression="tiff_adobe_deflate")
1022+
arguments = {"compression": "tiff_adobe_deflate"}
1023+
if argument:
1024+
arguments["strip_size"] = 2**18
1025+
im.save(out, **arguments)
10221026

10231027
with Image.open(out) as im:
10241028
assert len(im.tag_v2[STRIPOFFSETS]) == 1

src/PIL/PdfImagePlugin.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import os
2626
import time
2727

28-
from . import Image, ImageFile, ImageSequence, PdfParser, TiffImagePlugin, __version__
28+
from . import Image, ImageFile, ImageSequence, PdfParser, __version__
2929

3030
#
3131
# --------------------------------------------------------------------
@@ -181,10 +181,13 @@ def _save(im, fp, filename, save_all=False):
181181
if filter == "ASCIIHexDecode":
182182
ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)])
183183
elif filter == "CCITTFaxDecode":
184-
original_strip_size = TiffImagePlugin.STRIP_SIZE
185-
TiffImagePlugin.STRIP_SIZE = math.ceil(im.width / 8) * im.height
186-
im.save(op, "TIFF", compression="group4")
187-
TiffImagePlugin.STRIP_SIZE = original_strip_size
184+
im.save(
185+
op,
186+
"TIFF",
187+
compression="group4",
188+
# use a single strip
189+
strip_size=math.ceil(im.width / 8) * im.height,
190+
)
188191
elif filter == "DCTDecode":
189192
Image.SAVE["JPEG"](im, op, filename)
190193
elif filter == "FlateDecode":

src/PIL/TiffImagePlugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,8 @@ def _save(im, fp, filename):
16841684
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
16851685
# aim for given strip size (64 KB by default) when using libtiff writer
16861686
if libtiff:
1687-
rows_per_strip = 1 if stride == 0 else min(STRIP_SIZE // stride, im.size[1])
1687+
im_strip_size = encoderinfo.get("strip_size", STRIP_SIZE)
1688+
rows_per_strip = 1 if stride == 0 else min(im_strip_size // stride, im.size[1])
16881689
# JPEG encoder expects multiple of 8 rows
16891690
if compression == "jpeg":
16901691
rows_per_strip = min(((rows_per_strip + 7) // 8) * 8, im.size[1])

0 commit comments

Comments
 (0)