mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-19 12:33:16 +03:00
Merge pull request #7948 from radarhere/iptc
This commit is contained in:
commit
e62f47950e
|
@ -621,6 +621,19 @@ class TestFileTiff:
|
||||||
|
|
||||||
assert_image_equal_tofile(im, tmpfile)
|
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:
|
def test_rowsperstrip(self, tmp_path: Path) -> None:
|
||||||
outfile = str(tmp_path / "temp.tif")
|
outfile = str(tmp_path / "temp.tif")
|
||||||
im = hopper()
|
im = hopper()
|
||||||
|
|
|
@ -1658,6 +1658,16 @@ def _save(im, fp, filename):
|
||||||
except Exception:
|
except Exception:
|
||||||
pass # might not be an IFD. Might not have populated type
|
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
|
# additions written by Greg Couch, gregc@cgl.ucsf.edu
|
||||||
# inspired by image-sig posting from Kevin Cazabon, kcazabon@home.com
|
# inspired by image-sig posting from Kevin Cazabon, kcazabon@home.com
|
||||||
if hasattr(im, "tag_v2"):
|
if hasattr(im, "tag_v2"):
|
||||||
|
@ -1671,8 +1681,14 @@ def _save(im, fp, filename):
|
||||||
XMP,
|
XMP,
|
||||||
):
|
):
|
||||||
if key in im.tag_v2:
|
if key in im.tag_v2:
|
||||||
ifd[key] = im.tag_v2[key]
|
if key == IPTC_NAA_CHUNK and im.tag_v2.tagtype[key] not in (
|
||||||
ifd.tagtype[key] = im.tag_v2.tagtype[key]
|
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
|
# preserve ICC profile (should also work when saving other formats
|
||||||
# which support profiles as TIFF) -- 2008-06-06 Florian Hoech
|
# which support profiles as TIFF) -- 2008-06-06 Florian Hoech
|
||||||
|
@ -1812,16 +1828,6 @@ def _save(im, fp, filename):
|
||||||
# Merge the ones that we have with (optional) more bits from
|
# Merge the ones that we have with (optional) more bits from
|
||||||
# the original file, e.g x,y resolution so that we can
|
# the original file, e.g x,y resolution so that we can
|
||||||
# save(load('')) == original file.
|
# 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()):
|
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
|
||||||
# them to the custom dictionary.
|
# them to the custom dictionary.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user