Merge pull request #4560 from radarhere/xmp

This commit is contained in:
Hugo van Kemenade 2020-06-01 10:49:00 +03:00 committed by GitHub
commit 02d55b79c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 20 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

View File

@ -591,18 +591,27 @@ class TestFilePng:
with Image.open("Tests/images/hopper_idat_after_image_end.png") as im:
assert im.text == {"TXT": "VALUE", "ZIP": "VALUE"}
@pytest.mark.parametrize(
"test_file",
[
"Tests/images/exif.png", # With an EXIF chunk
"Tests/images/exif_imagemagick.png", # With an ImageMagick zTXt chunk
],
)
def test_exif(self, test_file):
with Image.open(test_file) as im:
def test_exif(self):
# With an EXIF chunk
with Image.open("Tests/images/exif.png") as im:
exif = im._getexif()
assert exif[274] == 1
# With an ImageMagick zTXt chunk
with Image.open("Tests/images/exif_imagemagick.png") as im:
exif = im._getexif()
assert exif[274] == 1
# Assert that info still can be extracted
# when the image is no longer a PngImageFile instance
exif = im.copy().getexif()
assert exif[274] == 1
# With XMP tags
with Image.open("Tests/images/xmp_tags_orientation.png") as im:
exif = im.getexif()
assert exif[274] == 3
def test_exif_save(self, tmp_path):
with Image.open("Tests/images/exif.png") as im:
test_file = str(tmp_path / "temp.png")

View File

@ -35,6 +35,7 @@ import struct
import sys
import tempfile
import warnings
import xml.etree.ElementTree
from collections.abc import Callable, MutableMapping
from pathlib import Path
@ -1300,7 +1301,28 @@ class Image:
def getexif(self):
if self._exif is None:
self._exif = Exif()
self._exif.load(self.info.get("exif"))
exif_info = self.info.get("exif")
if exif_info is None and "Raw profile type exif" in self.info:
exif_info = bytes.fromhex(
"".join(self.info["Raw profile type exif"].split("\n")[3:])
)
self._exif.load(exif_info)
# XMP tags
if 0x0112 not in self._exif:
xmp_tags = self.info.get("XML:com.adobe.xmp")
if xmp_tags:
root = xml.etree.ElementTree.fromstring(xmp_tags)
for elem in root.iter():
if elem.tag.endswith("}Description"):
orientation = elem.attrib.get(
"{http://ns.adobe.com/tiff/1.0/}Orientation"
)
if orientation:
self._exif[0x0112] = int(orientation)
break
return self._exif
def getim(self):

View File

@ -931,16 +931,7 @@ class PngImageFile(ImageFile.ImageFile):
if "exif" not in self.info:
self.load()
if self._exif is None:
self._exif = Image.Exif()
exif_info = self.info.get("exif")
if exif_info is None and "Raw profile type exif" in self.info:
exif_info = bytes.fromhex(
"".join(self.info["Raw profile type exif"].split("\n")[3:])
)
self._exif.load(exif_info)
return self._exif
return super().getexif()
def _close__fp(self):
try: