From 362ffaf9b7f063609bcd02ef30b1288e16e99cd1 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Sun, 18 Aug 2024 20:39:14 -0500 Subject: [PATCH 1/2] implement tiff exif multistrip support --- Tests/test_file_tiff.py | 3 ++- Tests/test_file_tiff_metadata.py | 22 ++++++++++++++++++++++ src/PIL/TiffImagePlugin.py | 8 +++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 190f83f40fc..44da25295ef 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -108,7 +108,8 @@ def test_bigtiff(self, tmp_path: Path) -> None: assert_image_equal_tofile(im, "Tests/images/hopper.tif") with Image.open("Tests/images/hopper_bigtiff.tif") as im: - # multistrip support not yet implemented + # The data type of this file's StripOffsets tag is LONG8, + # which is not yet supported for offset data when saving multiple frames. del im.tag_v2[273] outfile = str(tmp_path / "temp.tif") diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 1e0310001a5..00fe745cf59 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -181,6 +181,28 @@ def test_change_stripbytecounts_tag_type(tmp_path: Path) -> None: assert reloaded.tag_v2.tagtype[TiffImagePlugin.STRIPBYTECOUNTS] == TiffTags.LONG +def test_save_multiple_stripoffsets() -> None: + ifd = TiffImagePlugin.ImageFileDirectory_v2() + ifd[TiffImagePlugin.STRIPOFFSETS] = (123, 456) + assert ifd.tagtype[TiffImagePlugin.STRIPOFFSETS] == TiffTags.LONG + + # all values are in little-endian + assert ifd.tobytes() == ( + # number of tags == 1 + b"\x01\x00" + # tag id (2 bytes), type (2 bytes), count (4 bytes), value (4 bytes) + # == 273, 4, 2, 18 + # == TiffImagePlugin.STRIPOFFSETS, TiffTags.LONG, 2, 18 + # the value is the index of the tag data + b"\x11\x01\x04\x00\x02\x00\x00\x00\x12\x00\x00\x00" + # end of tags marker + b"\x00\x00\x00\x00" + # tag data == (149, 482) == (123 + 26, 456 + 26) + # 26 is the number of bytes before this data + b"\x95\x00\x00\x00\xe2\x01\x00\x00" + ) + + def test_no_duplicate_50741_tag() -> None: assert TAG_IDS["MakerNoteSafety"] == 50741 assert TAG_IDS["BestQualityScale"] == 50780 diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 14e6ea2786e..d4c46a79778 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -991,9 +991,11 @@ def tobytes(self, offset: int = 0) -> bytes: if stripoffsets is not None: tag, typ, count, value, data = entries[stripoffsets] if data: - msg = "multistrip support not yet implemented" - raise NotImplementedError(msg) - value = self._pack("L", self._unpack("L", value)[0] + offset) + size, handler = self._load_dispatch[typ] + values = [val + offset for val in handler(self, data, self.legacy_api)] + data = self._write_dispatch[typ](self, *values) + else: + value = self._pack("L", self._unpack("L", value)[0] + offset) entries[stripoffsets] = tag, typ, count, value, data # pass 2: write entries to file From 8b6fa92cc885cbd1d7b0beb60aaad9ab47298cbf Mon Sep 17 00:00:00 2001 From: Yay295 Date: Fri, 20 Sep 2024 08:39:11 -0500 Subject: [PATCH 2/2] rewrite some comments Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_file_tiff_metadata.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 00fe745cf59..36aabf4f82c 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -191,14 +191,15 @@ def test_save_multiple_stripoffsets() -> None: # number of tags == 1 b"\x01\x00" # tag id (2 bytes), type (2 bytes), count (4 bytes), value (4 bytes) - # == 273, 4, 2, 18 - # == TiffImagePlugin.STRIPOFFSETS, TiffTags.LONG, 2, 18 - # the value is the index of the tag data + # TiffImagePlugin.STRIPOFFSETS, TiffTags.LONG, 2, 18 + # where STRIPOFFSETS is 273, LONG is 4 + # and 18 is the offset of the tag data b"\x11\x01\x04\x00\x02\x00\x00\x00\x12\x00\x00\x00" - # end of tags marker + # end of entries b"\x00\x00\x00\x00" - # tag data == (149, 482) == (123 + 26, 456 + 26) - # 26 is the number of bytes before this data + # 26 is the total number of bytes output, + # the offset for any auxiliary strip data that will then be appended + # (123 + 26, 456 + 26) == (149, 482) b"\x95\x00\x00\x00\xe2\x01\x00\x00" )