Skip to content

Commit

Permalink
Merge pull request #5144 from UrielMaD/feature_xmp
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Apr 1, 2021
2 parents b90c73f + 7f5dbb7 commit 6812205
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Binary file added Tests/images/xmp_test.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions Tests/test_image_getxmp.py
@@ -0,0 +1,9 @@
from PIL import Image


def test_getxmp():
with Image.open("Tests/images/xmp_test.jpg") as im:
xmp = im.getxmp()

assert isinstance(xmp, dict)
assert xmp["Description"]["Version"] == "10.4"
22 changes: 22 additions & 0 deletions src/PIL/Image.py
Expand Up @@ -528,6 +528,7 @@ def __init__(self):
self.readonly = 0
self.pyaccess = None
self._exif = None
self._xmp = None

def __getattr__(self, name):
if name == "category":
Expand Down Expand Up @@ -1339,6 +1340,27 @@ def getexif(self):

return self._exif

def getxmp(self):
"""
Returns a dictionary containing the xmp tags for a given image.
:returns: XMP tags in a dictionary.
"""

if self._xmp is None:
self._xmp = {}

for segment, content in self.applist:
if segment == "APP1":
marker, xmp_tags = content.rsplit(b"\x00", 1)
if marker == b"http://ns.adobe.com/xap/1.0/":
root = xml.etree.ElementTree.fromstring(xmp_tags)
for element in root.findall(".//"):
self._xmp[element.tag.split("}")[1]] = {
child.split("}")[1]: value
for child, value in element.attrib.items()
}
return self._xmp

def getim(self):
"""
Returns a capsule that points to the internal image memory.
Expand Down

0 comments on commit 6812205

Please sign in to comment.