Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only preserve TIFF IPTC_NAA_CHUNK tag if type is BYTE or UNDEFINED #7948

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,19 @@ def test_roundtrip_tiff_uint16(self, tmp_path: Path) -> None:

assert_image_equal_tofile(im, tmpfile)

def test_iptc(self, tmp_path: Path) -> None:
# Do not preserve IPTC_NAA_CHUNK by default if type is LONG
outfile = str(tmp_path / "temp.tif")
im = hopper()
ifd = TiffImagePlugin.ImageFileDirectory_v2()
ifd[33723] = 1
ifd.tagtype[33723] = 4
im.tag_v2 = ifd
im.save(outfile)

with Image.open(outfile) as im:
assert 33723 not in im.tag_v2

def test_rowsperstrip(self, tmp_path: Path) -> None:
outfile = str(tmp_path / "temp.tif")
im = hopper()
Expand Down
30 changes: 18 additions & 12 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,16 @@ def _save(im, fp, filename):
except Exception:
pass # might not be an IFD. Might not have populated type

legacy_ifd = {}
if hasattr(im, "tag"):
legacy_ifd = im.tag.to_v2()

supplied_tags = {**legacy_ifd, **getattr(im, "tag_v2", {})}
if SAMPLEFORMAT in supplied_tags:
# SAMPLEFORMAT is determined by the image format and should not be copied
# from legacy_ifd.
del supplied_tags[SAMPLEFORMAT]

# additions written by Greg Couch, gregc@cgl.ucsf.edu
# inspired by image-sig posting from Kevin Cazabon, kcazabon@home.com
if hasattr(im, "tag_v2"):
Expand All @@ -1666,8 +1676,14 @@ def _save(im, fp, filename):
XMP,
):
if key in im.tag_v2:
ifd[key] = im.tag_v2[key]
ifd.tagtype[key] = im.tag_v2.tagtype[key]
if key == IPTC_NAA_CHUNK and im.tag_v2.tagtype[key] not in (
TiffTags.BYTE,
TiffTags.UNDEFINED,
):
del supplied_tags[key]
else:
ifd[key] = im.tag_v2[key]
ifd.tagtype[key] = im.tag_v2.tagtype[key]

# preserve ICC profile (should also work when saving other formats
# which support profiles as TIFF) -- 2008-06-06 Florian Hoech
Expand Down Expand Up @@ -1807,16 +1823,6 @@ def _save(im, fp, filename):
# Merge the ones that we have with (optional) more bits from
# the original file, e.g x,y resolution so that we can
# save(load('')) == original file.
legacy_ifd = {}
if hasattr(im, "tag"):
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}
if SAMPLEFORMAT in supplied_tags:
del supplied_tags[SAMPLEFORMAT]

for tag, value in itertools.chain(ifd.items(), supplied_tags.items()):
# Libtiff can only process certain core items without adding
# them to the custom dictionary.
Expand Down