Skip to content

Commit e62f479

Browse files
authored
Merge pull request #7948 from radarhere/iptc
2 parents 3168140 + 2245df0 commit e62f479

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
@@ -1658,6 +1658,16 @@ def _save(im, fp, filename):
16581658
except Exception:
16591659
pass # might not be an IFD. Might not have populated type
16601660

1661+
legacy_ifd = {}
1662+
if hasattr(im, "tag"):
1663+
legacy_ifd = im.tag.to_v2()
1664+
1665+
supplied_tags = {**legacy_ifd, **getattr(im, "tag_v2", {})}
1666+
if SAMPLEFORMAT in supplied_tags:
1667+
# SAMPLEFORMAT is determined by the image format and should not be copied
1668+
# from legacy_ifd.
1669+
del supplied_tags[SAMPLEFORMAT]
1670+
16611671
# additions written by Greg Couch, [email protected]
16621672
# inspired by image-sig posting from Kevin Cazabon, [email protected]
16631673
if hasattr(im, "tag_v2"):
@@ -1671,8 +1681,14 @@ def _save(im, fp, filename):
16711681
XMP,
16721682
):
16731683
if key in im.tag_v2:
1674-
ifd[key] = im.tag_v2[key]
1675-
ifd.tagtype[key] = im.tag_v2.tagtype[key]
1684+
if key == IPTC_NAA_CHUNK and im.tag_v2.tagtype[key] not in (
1685+
TiffTags.BYTE,
1686+
TiffTags.UNDEFINED,
1687+
):
1688+
del supplied_tags[key]
1689+
else:
1690+
ifd[key] = im.tag_v2[key]
1691+
ifd.tagtype[key] = im.tag_v2.tagtype[key]
16761692

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

0 commit comments

Comments
 (0)