Skip to content

Commit 2245df0

Browse files
committed
Only preserve IPTC_NAA_CHUNK tag if type is BYTE or UNDEFINED
1 parent f8ec9f7 commit 2245df0

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

Tests/test_file_tiff.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,19 @@ def test_roundtrip_tiff_uint16(self, tmp_path: Path) -> None:
621621

622622
assert_image_equal_tofile(im, tmpfile)
623623

624+
def test_iptc(self, tmp_path: Path) -> None:
625+
# Do not preserve IPTC_NAA_CHUNK by default if type is LONG
626+
outfile = str(tmp_path / "temp.tif")
627+
im = hopper()
628+
ifd = TiffImagePlugin.ImageFileDirectory_v2()
629+
ifd[33723] = 1
630+
ifd.tagtype[33723] = 4
631+
im.tag_v2 = ifd
632+
im.save(outfile)
633+
634+
with Image.open(outfile) as im:
635+
assert 33723 not in im.tag_v2
636+
624637
def test_rowsperstrip(self, tmp_path: Path) -> None:
625638
outfile = str(tmp_path / "temp.tif")
626639
im = hopper()

src/PIL/TiffImagePlugin.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,16 @@ def _save(im, fp, filename):
16531653
except Exception:
16541654
pass # might not be an IFD. Might not have populated type
16551655

1656+
legacy_ifd = {}
1657+
if hasattr(im, "tag"):
1658+
legacy_ifd = im.tag.to_v2()
1659+
1660+
supplied_tags = {**legacy_ifd, **getattr(im, "tag_v2", {})}
1661+
if SAMPLEFORMAT in supplied_tags:
1662+
# SAMPLEFORMAT is determined by the image format and should not be copied
1663+
# from legacy_ifd.
1664+
del supplied_tags[SAMPLEFORMAT]
1665+
16561666
# additions written by Greg Couch, [email protected]
16571667
# inspired by image-sig posting from Kevin Cazabon, [email protected]
16581668
if hasattr(im, "tag_v2"):
@@ -1666,8 +1676,14 @@ def _save(im, fp, filename):
16661676
XMP,
16671677
):
16681678
if key in im.tag_v2:
1669-
ifd[key] = im.tag_v2[key]
1670-
ifd.tagtype[key] = im.tag_v2.tagtype[key]
1679+
if key == IPTC_NAA_CHUNK and im.tag_v2.tagtype[key] not in (
1680+
TiffTags.BYTE,
1681+
TiffTags.UNDEFINED,
1682+
):
1683+
del supplied_tags[key]
1684+
else:
1685+
ifd[key] = im.tag_v2[key]
1686+
ifd.tagtype[key] = im.tag_v2.tagtype[key]
16711687

16721688
# preserve ICC profile (should also work when saving other formats
16731689
# which support profiles as TIFF) -- 2008-06-06 Florian Hoech
@@ -1807,16 +1823,6 @@ def _save(im, fp, filename):
18071823
# Merge the ones that we have with (optional) more bits from
18081824
# the original file, e.g x,y resolution so that we can
18091825
# save(load('')) == original file.
1810-
legacy_ifd = {}
1811-
if hasattr(im, "tag"):
1812-
legacy_ifd = im.tag.to_v2()
1813-
1814-
# SAMPLEFORMAT is determined by the image format and should not be copied
1815-
# from legacy_ifd.
1816-
supplied_tags = {**getattr(im, "tag_v2", {}), **legacy_ifd}
1817-
if SAMPLEFORMAT in supplied_tags:
1818-
del supplied_tags[SAMPLEFORMAT]
1819-
18201826
for tag, value in itertools.chain(ifd.items(), supplied_tags.items()):
18211827
# Libtiff can only process certain core items without adding
18221828
# them to the custom dictionary.

0 commit comments

Comments
 (0)