Added strip_size as TIFF encoder argument

This commit is contained in:
Andrew Murray 2022-08-01 21:41:17 +10:00
parent f5b27f90f7
commit 2b14d83549
3 changed files with 18 additions and 10 deletions

View File

@ -1011,14 +1011,18 @@ class TestFileLibTiff(LibTiffTestCase):
# Assert that there are multiple strips # Assert that there are multiple strips
assert len(im.tag_v2[STRIPOFFSETS]) > 1 assert len(im.tag_v2[STRIPOFFSETS]) > 1
def test_save_single_strip(self, tmp_path): @pytest.mark.parametrize("argument", (True, False))
def test_save_single_strip(self, argument, tmp_path):
im = hopper("RGB").resize((256, 256)) im = hopper("RGB").resize((256, 256))
out = str(tmp_path / "temp.tif") out = str(tmp_path / "temp.tif")
TiffImagePlugin.STRIP_SIZE = 2**18 if not argument:
TiffImagePlugin.STRIP_SIZE = 2**18
try: try:
arguments = {"compression": "tiff_adobe_deflate"}
im.save(out, compression="tiff_adobe_deflate") if argument:
arguments["strip_size"] = 2**18
im.save(out, **arguments)
with Image.open(out) as im: with Image.open(out) as im:
assert len(im.tag_v2[STRIPOFFSETS]) == 1 assert len(im.tag_v2[STRIPOFFSETS]) == 1

View File

@ -25,7 +25,7 @@ import math
import os import os
import time import time
from . import Image, ImageFile, ImageSequence, PdfParser, TiffImagePlugin, __version__ from . import Image, ImageFile, ImageSequence, PdfParser, __version__
# #
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -181,10 +181,13 @@ def _save(im, fp, filename, save_all=False):
if filter == "ASCIIHexDecode": if filter == "ASCIIHexDecode":
ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)]) ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)])
elif filter == "CCITTFaxDecode": elif filter == "CCITTFaxDecode":
original_strip_size = TiffImagePlugin.STRIP_SIZE im.save(
TiffImagePlugin.STRIP_SIZE = math.ceil(im.width / 8) * im.height op,
im.save(op, "TIFF", compression="group4") "TIFF",
TiffImagePlugin.STRIP_SIZE = original_strip_size compression="group4",
# use a single strip
strip_size=math.ceil(im.width / 8) * im.height,
)
elif filter == "DCTDecode": elif filter == "DCTDecode":
Image.SAVE["JPEG"](im, op, filename) Image.SAVE["JPEG"](im, op, filename)
elif filter == "FlateDecode": elif filter == "FlateDecode":

View File

@ -1684,7 +1684,8 @@ def _save(im, fp, filename):
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8) stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
# aim for given strip size (64 KB by default) when using libtiff writer # aim for given strip size (64 KB by default) when using libtiff writer
if libtiff: if libtiff:
rows_per_strip = 1 if stride == 0 else min(STRIP_SIZE // stride, im.size[1]) im_strip_size = encoderinfo.get("strip_size", STRIP_SIZE)
rows_per_strip = 1 if stride == 0 else min(im_strip_size // stride, im.size[1])
# JPEG encoder expects multiple of 8 rows # JPEG encoder expects multiple of 8 rows
if compression == "jpeg": if compression == "jpeg":
rows_per_strip = min(((rows_per_strip + 7) // 8) * 8, im.size[1]) rows_per_strip = min(((rows_per_strip + 7) // 8) * 8, im.size[1])