Corrected getxmp() descending into XML

This commit is contained in:
Andrew Murray 2021-04-30 14:07:09 +10:00
parent 20bd9e9261
commit c1fbe2d975
2 changed files with 32 additions and 6 deletions

View File

@ -828,7 +828,16 @@ class TestFileJpeg:
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")

View File

@ -492,12 +492,29 @@ class JpegImageFile(ImageFile.ImageFile):
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