From 63879f04b119551a3d562dd2da24464e199b9ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Komar=C4=8Devi=C4=87?= <4973094+kmilos@users.noreply.github.com> Date: Fri, 1 Oct 2021 13:50:02 +0200 Subject: [PATCH 1/4] Make TIFF strip size configurable --- src/PIL/TiffImagePlugin.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index eb33e3218..ab9ccd1cd 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -58,6 +58,7 @@ logger = logging.getLogger(__name__) 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) @@ -1617,9 +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 = min((2 ** 16 + stride - 1) // stride, im.size[1]) + rows_per_strip = 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]) From b5d6a73da9533ed5510383a6302fa1f17bdfe4a4 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 8 Oct 2021 12:53:53 +1100 Subject: [PATCH 2/4] Added test --- Tests/test_file_libtiff.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 1d0c93f06..ebc8e5e07 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -986,3 +986,16 @@ class TestFileLibTiff(LibTiffTestCase): with Image.open(out) as im: # 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: + with Image.open(out) as im: + assert len(im.tag_v2[STRIPOFFSETS]) == 1 + finally: + TiffImagePlugin.STRIP_SIZE = 65536 From 1140f6538d03ef0b481773edcc0f8c90f992c7f1 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Thu, 14 Oct 2021 08:09:36 +1100 Subject: [PATCH 3/4] Ensure reset after test failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondrej Baranovič --- Tests/test_file_libtiff.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 2c12efb81..aed1cabc2 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -992,9 +992,10 @@ class TestFileLibTiff(LibTiffTestCase): out = str(tmp_path / "temp.tif") TiffImagePlugin.STRIP_SIZE = 2 ** 18 - im.save(out, compression="tiff_adobe_deflate") - try: + + im.save(out, compression="tiff_adobe_deflate") + with Image.open(out) as im: assert len(im.tag_v2[STRIPOFFSETS]) == 1 finally: From dbb0a41600ee26faa00d3eaacb6eb37b078493eb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Oct 2021 21:10:19 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Tests/test_file_libtiff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index aed1cabc2..6ac90f778 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -993,7 +993,7 @@ class TestFileLibTiff(LibTiffTestCase): TiffImagePlugin.STRIP_SIZE = 2 ** 18 try: - + im.save(out, compression="tiff_adobe_deflate") with Image.open(out) as im: