Merge pull request #7654 from radarhere/rowsperstrip

This commit is contained in:
Hugo van Kemenade 2023-12-31 17:29:26 +02:00 committed by GitHub
commit 5df7235d90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 14 deletions

View File

@ -612,6 +612,14 @@ class TestFileTiff:
assert_image_equal_tofile(im, tmpfile) assert_image_equal_tofile(im, tmpfile)
def test_rowsperstrip(self, tmp_path):
outfile = str(tmp_path / "temp.tif")
im = hopper()
im.save(outfile, tiffinfo={278: 256})
with Image.open(outfile) as im:
assert im.tag_v2[278] == 256
def test_strip_raw(self): def test_strip_raw(self):
infile = "Tests/images/tiff_strip_raw.tif" infile = "Tests/images/tiff_strip_raw.tif"
with Image.open(infile) as im: with Image.open(infile) as im:

View File

@ -123,6 +123,7 @@ def test_write_metadata(tmp_path):
"""Test metadata writing through the python code""" """Test metadata writing through the python code"""
with Image.open("Tests/images/hopper.tif") as img: with Image.open("Tests/images/hopper.tif") as img:
f = str(tmp_path / "temp.tiff") f = str(tmp_path / "temp.tiff")
del img.tag[278]
img.save(f, tiffinfo=img.tag) img.save(f, tiffinfo=img.tag)
original = img.tag_v2.named() original = img.tag_v2.named()
@ -159,6 +160,7 @@ def test_change_stripbytecounts_tag_type(tmp_path):
out = str(tmp_path / "temp.tiff") out = str(tmp_path / "temp.tiff")
with Image.open("Tests/images/hopper.tif") as im: with Image.open("Tests/images/hopper.tif") as im:
info = im.tag_v2 info = im.tag_v2
del info[278]
# Resize the image so that STRIPBYTECOUNTS will be larger than a SHORT # Resize the image so that STRIPBYTECOUNTS will be larger than a SHORT
im = im.resize((500, 500)) im = im.resize((500, 500))

View File

@ -1705,10 +1705,13 @@ def _save(im, fp, filename):
ifd[COLORMAP] = colormap ifd[COLORMAP] = colormap
# data orientation # data orientation
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8) stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
if ROWSPERSTRIP not in ifd:
# 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:
im_strip_size = encoderinfo.get("strip_size", STRIP_SIZE) 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]) 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])
@ -1716,9 +1719,9 @@ def _save(im, fp, filename):
rows_per_strip = im.size[1] rows_per_strip = im.size[1]
if rows_per_strip == 0: if rows_per_strip == 0:
rows_per_strip = 1 rows_per_strip = 1
strip_byte_counts = 1 if stride == 0 else stride * rows_per_strip
strips_per_image = (im.size[1] + rows_per_strip - 1) // rows_per_strip
ifd[ROWSPERSTRIP] = rows_per_strip ifd[ROWSPERSTRIP] = rows_per_strip
strip_byte_counts = 1 if stride == 0 else stride * ifd[ROWSPERSTRIP]
strips_per_image = (im.size[1] + ifd[ROWSPERSTRIP] - 1) // ifd[ROWSPERSTRIP]
if strip_byte_counts >= 2**16: if strip_byte_counts >= 2**16:
ifd.tagtype[STRIPBYTECOUNTS] = TiffTags.LONG ifd.tagtype[STRIPBYTECOUNTS] = TiffTags.LONG
ifd[STRIPBYTECOUNTS] = (strip_byte_counts,) * (strips_per_image - 1) + ( ifd[STRIPBYTECOUNTS] = (strip_byte_counts,) * (strips_per_image - 1) + (