Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TIFF strip size configurable #5744

Merged
merged 5 commits into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions Tests/test_file_libtiff.py
Expand Up @@ -987,6 +987,19 @@ def test_save_multistrip(self, compression, tmp_path):
# Assert that there are multiple strips
assert len(im.tag_v2[STRIPOFFSETS]) > 1

def test_save_single_strip(self, tmp_path):
im = hopper("RGB").resize((256, 256))
out = str(tmp_path / "temp.tif")

TiffImagePlugin.STRIP_SIZE = 2 ** 18
im.save(out, compression="tiff_adobe_deflate")

try:
radarhere marked this conversation as resolved.
Show resolved Hide resolved
with Image.open(out) as im:
assert len(im.tag_v2[STRIPOFFSETS]) == 1
finally:
TiffImagePlugin.STRIP_SIZE = 65536

@pytest.mark.parametrize("compression", ("tiff_adobe_deflate", None))
def test_save_zero(self, compression, tmp_path):
im = Image.new("RGB", (0, 0))
Expand Down
7 changes: 3 additions & 4 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -58,6 +58,7 @@
READ_LIBTIFF = False
WRITE_LIBTIFF = False
IFD_LEGACY_API = True
STRIP_SIZE = 65536

II = b"II" # little-endian (Intel style)
MM = b"MM" # big-endian (Motorola style)
Expand Down Expand Up @@ -1617,11 +1618,9 @@ def _save(im, fp, filename):
ifd[COLORMAP] = tuple(v * 256 for v in lut)
# data orientation
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
# aim for 64 KB strips when using libtiff writer
# aim for given strip size (64 KB by default) when using libtiff writer
if libtiff:
rows_per_strip = (
1 if stride == 0 else min((2 ** 16 + stride - 1) // stride, im.size[1])
)
rows_per_strip = 1 if stride == 0 else min(STRIP_SIZE // 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])
Expand Down