Parse orientation from XMP tags

This commit is contained in:
Andrew Murray 2020-04-16 21:05:34 +10:00
parent 97280a045c
commit 1e63f772f8
4 changed files with 27 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

View File

@ -603,6 +603,11 @@ class TestFilePng:
exif = im._getexif() exif = im._getexif()
assert exif[274] == 1 assert exif[274] == 1
def test_xmp_tags_orientation(self):
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): def test_exif_save(self, tmp_path):
with Image.open("Tests/images/exif.png") as im: with Image.open("Tests/images/exif.png") as im:
test_file = str(tmp_path / "temp.png") test_file = str(tmp_path / "temp.png")

View File

@ -35,6 +35,7 @@ import struct
import sys import sys
import tempfile import tempfile
import warnings import warnings
import xml.etree.ElementTree
from collections.abc import Callable, MutableMapping from collections.abc import Callable, MutableMapping
from pathlib import Path from pathlib import Path
@ -1297,10 +1298,30 @@ class Image:
return tuple(extrema) return tuple(extrema)
return self.im.getextrema() return self.im.getextrema()
def _parse_xmp_tags(self):
if 0x0112 in self._exif:
return
xmp_tags = self.info.get("XML:com.adobe.xmp")
if not xmp_tags:
return
root = xml.etree.ElementTree.fromstring(xmp_tags)
for elem in root.iter():
if elem.tag.endswith("}Description"):
break
else:
return
orientation = elem.attrib.get("{http://ns.adobe.com/tiff/1.0/}Orientation")
if orientation:
self._exif[0x0112] = int(orientation)
def getexif(self): def getexif(self):
if self._exif is None: if self._exif is None:
self._exif = Exif() self._exif = Exif()
self._exif.load(self.info.get("exif")) self._exif.load(self.info.get("exif"))
self._parse_xmp_tags()
return self._exif return self._exif
def getim(self): def getim(self):

View File

@ -940,6 +940,7 @@ class PngImageFile(ImageFile.ImageFile):
"".join(self.info["Raw profile type exif"].split("\n")[3:]) "".join(self.info["Raw profile type exif"].split("\n")[3:])
) )
self._exif.load(exif_info) self._exif.load(exif_info)
self._parse_xmp_tags()
return self._exif return self._exif
def _close__fp(self): def _close__fp(self):