mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-29 03:16:18 +03:00
Merge pull request #5575 from radarhere/tiff_exif
Added "exif" keyword argument to TIFF saving
This commit is contained in:
commit
ba7502c056
|
@ -403,10 +403,8 @@ class TestFileTiff:
|
||||||
with Image.open("Tests/images/ifd_tag_type.tiff") as im:
|
with Image.open("Tests/images/ifd_tag_type.tiff") as im:
|
||||||
assert 0x8825 in im.tag_v2
|
assert 0x8825 in im.tag_v2
|
||||||
|
|
||||||
def test_exif(self):
|
def test_exif(self, tmp_path):
|
||||||
with Image.open("Tests/images/ifd_tag_type.tiff") as im:
|
def check_exif(exif):
|
||||||
exif = im.getexif()
|
|
||||||
|
|
||||||
assert sorted(exif.keys()) == [
|
assert sorted(exif.keys()) == [
|
||||||
256,
|
256,
|
||||||
257,
|
257,
|
||||||
|
@ -439,6 +437,24 @@ class TestFileTiff:
|
||||||
assert gps[0] == b"\x03\x02\x00\x00"
|
assert gps[0] == b"\x03\x02\x00\x00"
|
||||||
assert gps[18] == "WGS-84"
|
assert gps[18] == "WGS-84"
|
||||||
|
|
||||||
|
outfile = str(tmp_path / "temp.tif")
|
||||||
|
with Image.open("Tests/images/ifd_tag_type.tiff") as im:
|
||||||
|
exif = im.getexif()
|
||||||
|
check_exif(exif)
|
||||||
|
|
||||||
|
im.save(outfile, exif=exif)
|
||||||
|
|
||||||
|
outfile2 = str(tmp_path / "temp2.tif")
|
||||||
|
with Image.open(outfile) as im:
|
||||||
|
exif = im.getexif()
|
||||||
|
check_exif(exif)
|
||||||
|
|
||||||
|
im.save(outfile2, exif=exif.tobytes())
|
||||||
|
|
||||||
|
with Image.open(outfile2) as im:
|
||||||
|
exif = im.getexif()
|
||||||
|
check_exif(exif)
|
||||||
|
|
||||||
def test_exif_frames(self):
|
def test_exif_frames(self):
|
||||||
# Test that EXIF data can change across frames
|
# Test that EXIF data can change across frames
|
||||||
with Image.open("Tests/images/g4-multi.tiff") as im:
|
with Image.open("Tests/images/g4-multi.tiff") as im:
|
||||||
|
|
|
@ -903,6 +903,11 @@ The :py:meth:`~PIL.Image.Image.save` method can take the following keyword argum
|
||||||
require a matching type in
|
require a matching type in
|
||||||
:py:attr:`~PIL.TiffImagePlugin.ImageFileDirectory_v2.tagtype` tagtype.
|
:py:attr:`~PIL.TiffImagePlugin.ImageFileDirectory_v2.tagtype` tagtype.
|
||||||
|
|
||||||
|
**exif**
|
||||||
|
Alternate keyword to "tiffinfo", for consistency with other formats.
|
||||||
|
|
||||||
|
.. versionadded:: 8.4.0
|
||||||
|
|
||||||
**compression**
|
**compression**
|
||||||
A string containing the desired compression method for the
|
A string containing the desired compression method for the
|
||||||
file. (valid only with libtiff installed) Valid compression
|
file. (valid only with libtiff installed) Valid compression
|
||||||
|
|
|
@ -1143,6 +1143,17 @@ class TiffImageFile(ImageFile.ImageFile):
|
||||||
if not self.is_animated:
|
if not self.is_animated:
|
||||||
self._close_exclusive_fp_after_loading = True
|
self._close_exclusive_fp_after_loading = True
|
||||||
|
|
||||||
|
# reset buffered io handle in case fp
|
||||||
|
# was passed to libtiff, invalidating the buffer
|
||||||
|
self.fp.tell()
|
||||||
|
|
||||||
|
# load IFD data from fp before it is closed
|
||||||
|
exif = self.getexif()
|
||||||
|
for key in TiffTags.TAGS_V2_GROUPS.keys():
|
||||||
|
if key not in exif:
|
||||||
|
continue
|
||||||
|
exif.get_ifd(key)
|
||||||
|
|
||||||
def _load_libtiff(self):
|
def _load_libtiff(self):
|
||||||
"""Overload method triggered when we detect a compressed tiff
|
"""Overload method triggered when we detect a compressed tiff
|
||||||
Calls out to libtiff"""
|
Calls out to libtiff"""
|
||||||
|
@ -1507,12 +1518,24 @@ def _save(im, fp, filename):
|
||||||
ifd[IMAGELENGTH] = im.size[1]
|
ifd[IMAGELENGTH] = im.size[1]
|
||||||
|
|
||||||
# write any arbitrary tags passed in as an ImageFileDirectory
|
# write any arbitrary tags passed in as an ImageFileDirectory
|
||||||
info = encoderinfo.get("tiffinfo", {})
|
if "tiffinfo" in encoderinfo:
|
||||||
|
info = encoderinfo["tiffinfo"]
|
||||||
|
elif "exif" in encoderinfo:
|
||||||
|
info = encoderinfo["exif"]
|
||||||
|
if isinstance(info, bytes):
|
||||||
|
exif = Image.Exif()
|
||||||
|
exif.load(info)
|
||||||
|
info = exif
|
||||||
|
else:
|
||||||
|
info = {}
|
||||||
logger.debug("Tiffinfo Keys: %s" % list(info))
|
logger.debug("Tiffinfo Keys: %s" % list(info))
|
||||||
if isinstance(info, ImageFileDirectory_v1):
|
if isinstance(info, ImageFileDirectory_v1):
|
||||||
info = info.to_v2()
|
info = info.to_v2()
|
||||||
for key in info:
|
for key in info:
|
||||||
ifd[key] = info.get(key)
|
if isinstance(info, Image.Exif) and key in TiffTags.TAGS_V2_GROUPS.keys():
|
||||||
|
ifd[key] = info.get_ifd(key)
|
||||||
|
else:
|
||||||
|
ifd[key] = info.get(key)
|
||||||
try:
|
try:
|
||||||
ifd.tagtype[key] = info.tagtype[key]
|
ifd.tagtype[key] = info.tagtype[key]
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user