Merge pull request #5426 from radarhere/undefined_zero

Fixed UNDEFINED TIFF tag of length 0 being changed in roundtrip
This commit is contained in:
Hugo van Kemenade 2021-05-03 09:07:32 +03:00 committed by GitHub
commit d2f2fbacaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -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)

View File

@ -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]