Skip to content

Commit

Permalink
Merge pull request #6758 from radarhere/webp_getxmp
Browse files Browse the repository at this point in the history
Resolves #6757
  • Loading branch information
hugovk committed Nov 26, 2022
2 parents ad938d5 + 3f94103 commit 066c3ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Tests/test_file_webp_metadata.py
Expand Up @@ -11,6 +11,11 @@
skip_unless_feature("webp_mux"),
]

try:
from defusedxml import ElementTree
except ImportError:
ElementTree = None


def test_read_exif_metadata():

Expand Down Expand Up @@ -110,6 +115,22 @@ def test_read_no_exif():
assert not webp_image._getexif()


def test_getxmp():
with Image.open("Tests/images/flower.webp") as im:
assert "xmp" not in im.info
assert im.getxmp() == {}

with Image.open("Tests/images/flower2.webp") as im:
if ElementTree is None:
with pytest.warns(UserWarning):
assert im.getxmp() == {}
else:
assert (
im.getxmp()["xmpmeta"]["xmptk"]
== "Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27 "
)


@skip_unless_feature("webp_anim")
def test_write_animated_metadata(tmp_path):
iccp_data = b"<iccp_data>"
Expand Down
6 changes: 6 additions & 0 deletions docs/releasenotes/9.4.0.rst
Expand Up @@ -45,6 +45,12 @@ removes the hidden RGB values for better compression by default in libwebp 0.5
or later. By setting this option to ``True``, the encoder will keep the hidden
RGB values.

getxmp()
^^^^^^^^

`XMP data <https://en.wikipedia.org/wiki/Extensible_Metadata_Platform>`_ can now be
decoded for WEBP images through ``getxmp()``.

Security
========

Expand Down
9 changes: 9 additions & 0 deletions src/PIL/WebPImagePlugin.py
Expand Up @@ -98,6 +98,15 @@ def _getexif(self):
return None
return self.getexif()._get_merged_dict()

def getxmp(self):
"""
Returns a dictionary containing the XMP tags.
Requires defusedxml to be installed.
:returns: XMP tags in a dictionary.
"""
return self._getxmp(self.info["xmp"]) if "xmp" in self.info else {}

def seek(self, frame):
if not self._seek_check(frame):
return
Expand Down

0 comments on commit 066c3ab

Please sign in to comment.