Skip to content

Commit

Permalink
Corrected getxmp() descending into XML
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 30, 2021
1 parent ef9a8e5 commit 217c59f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
11 changes: 10 additions & 1 deletion Tests/test_file_jpeg.py
Expand Up @@ -837,7 +837,16 @@ def test_getxmp(self):
xmp = im.getxmp()

assert isinstance(xmp, dict)
assert xmp["Description"]["Version"] == "10.4"

description = xmp["xmpmeta"]["RDF"]["Description"]
assert description["DerivedFrom"] is None
assert (
description["Look"]["Description"]["Group"]["Alt"]["li"] == "Profiles"
)
assert description["ToneCurve"]["Seq"]["li"] == ["0, 0", "255, 255"]

# Attribute
assert description["Version"] == "10.4"


@pytest.mark.skipif(not is_win32(), reason="Windows only")
Expand Down
27 changes: 22 additions & 5 deletions src/PIL/JpegImagePlugin.py
Expand Up @@ -489,12 +489,29 @@ def getxmp(self):
if segment == "APP1":
marker, xmp_tags = content.rsplit(b"\x00", 1)
if marker == b"http://ns.adobe.com/xap/1.0/":

def get_name(tag):
return tag.split("}")[1]

def get_value(element):
children = list(element)
if children:
value = {get_name(k): v for k, v in element.attrib.items()}
for child in children:
name = get_name(child.tag)
child_value = get_value(child)
if name in value:
if not isinstance(value[name], list):
value[name] = [value[name]]
value[name].append(child_value)
else:
value[name] = child_value
return value
else:
return element.text

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()
}
self._xmp[get_name(root.tag)] = get_value(root)
return self._xmp


Expand Down

0 comments on commit 217c59f

Please sign in to comment.