Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getxmp() method #5144

Merged
merged 24 commits into from Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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():
im = Image.open("Tests/images/xmp_test.jpg")
type_repr = repr(type(im.getxmp()))

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

@property
def width(self):
Expand Down Expand Up @@ -1318,6 +1319,27 @@ def getexif(self):

return self._exif

def getxmp(self):
"""
Returns an object containing the xmp tags for a given image.
:returns: XMP tags in an object.
UrielMaD marked this conversation as resolved.
Show resolved Hide resolved
"""

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(".//"):
xmp_atribs = []
for child, value in element.attrib.items():
xmp_atribs.append({child.split("}")[1]: value})
UrielMaD marked this conversation as resolved.
Show resolved Hide resolved
self._xmp.update({element.tag.split("}")[1]: xmp_atribs})
return self._xmp

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