From 976d9fa347a9f477b479e2c3fdb8cc8d284b0f25 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 25 Apr 2021 21:37:45 +1000 Subject: [PATCH] Fixed UNDEFINED TIFF tag of length 0 being changed in roundtrip --- Tests/test_file_tiff_metadata.py | 15 +++++++++++++++ src/PIL/TiffImagePlugin.py | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index af7528be0..e2841163b 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -185,6 +185,21 @@ def test_iptc(tmp_path): im.save(out) +def test_undefined_zero(tmp_path): + # Check that the tag has not been changed since this test was created + tag = TiffTags.TAGS_V2[45059] + assert tag.type == TiffTags.UNDEFINED + assert tag.length == 0 + + info = TiffImagePlugin.ImageFileDirectory(b"II*\x00\x08\x00\x00\x00") + info[45059] = b"test" + + # Assert that the tag value does not change by setting it to itself + original = info[45059] + info[45059] = info[45059] + assert info[45059] == original + + def test_empty_metadata(): f = io.BytesIO(b"II*\x00\x08\x00\x00\x00") head = f.read(8) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 9d821dcf9..17e29ba4c 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -565,7 +565,8 @@ class ImageFileDirectory_v2(MutableMapping): if self.tagtype[tag] == TiffTags.UNDEFINED: values = [ - value.encode("ascii", "replace") if isinstance(value, str) else value + v.encode("ascii", "replace") if isinstance(v, str) else v + for v in values ] elif self.tagtype[tag] == TiffTags.RATIONAL: values = [float(v) if isinstance(v, int) else v for v in values]