Do not preserve EXIFIFD tag by default

This commit is contained in:
Andrew Murray 2024-06-07 13:25:56 +10:00
parent 2d0610888e
commit ac7967cfc0
2 changed files with 17 additions and 8 deletions

View File

@ -685,13 +685,18 @@ class TestFileLibTiff(LibTiffTestCase):
assert reloaded.tag_v2[530] == (1, 1) assert reloaded.tag_v2[530] == (1, 1)
assert reloaded.tag_v2[532] == (0, 255, 128, 255, 128, 255) assert reloaded.tag_v2[532] == (0, 255, 128, 255, 128, 255)
def test_exif_ifd(self, tmp_path: Path) -> None: def test_exif_ifd(self) -> None:
outfile = str(tmp_path / "temp.tif") out = io.BytesIO()
with Image.open("Tests/images/tiff_adobe_deflate.tif") as im: with Image.open("Tests/images/tiff_adobe_deflate.tif") as im:
assert im.tag_v2[34665] == 125456 assert im.tag_v2[34665] == 125456
im.save(outfile) im.save(out, "TIFF")
with Image.open(outfile) as reloaded: with Image.open(out) as reloaded:
assert 34665 not in reloaded.tag_v2
im.save(out, "TIFF", tiffinfo={34665: 125456})
with Image.open(out) as reloaded:
if Image.core.libtiff_support_custom_tags: if Image.core.libtiff_support_custom_tags:
assert reloaded.tag_v2[34665] == 125456 assert reloaded.tag_v2[34665] == 125456

View File

@ -1816,11 +1816,15 @@ def _save(im, fp, filename):
if hasattr(im, "tag"): if hasattr(im, "tag"):
legacy_ifd = im.tag.to_v2() legacy_ifd = im.tag.to_v2()
# SAMPLEFORMAT is determined by the image format and should not be copied
# from legacy_ifd.
supplied_tags = {**getattr(im, "tag_v2", {}), **legacy_ifd} supplied_tags = {**getattr(im, "tag_v2", {}), **legacy_ifd}
if SAMPLEFORMAT in supplied_tags: for tag in (
del supplied_tags[SAMPLEFORMAT] # IFD offset that may not be correct in the saved image
EXIFIFD,
# Determined by the image format and should not be copied from legacy_ifd.
SAMPLEFORMAT,
):
if tag in supplied_tags:
del supplied_tags[tag]
for tag, value in itertools.chain(ifd.items(), supplied_tags.items()): for tag, value in itertools.chain(ifd.items(), supplied_tags.items()):
# Libtiff can only process certain core items without adding # Libtiff can only process certain core items without adding