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() to WebPImagePlugin #6758

Merged
merged 3 commits into from Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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