Skip to content

Commit

Permalink
Merge pull request #8171 from radarhere/xmp
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jun 27, 2024
2 parents a90a9d5 + fd3f6c1 commit 6a44854
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile
import warnings
from pathlib import Path
from types import ModuleType
from typing import IO, Any

import pytest
Expand Down Expand Up @@ -35,6 +36,12 @@
skip_unless_feature,
)

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


# Deprecation helper
def helper_image_new(mode: str, size: tuple[int, int]) -> Image.Image:
Expand Down Expand Up @@ -921,6 +928,21 @@ def test_empty_xmp(self) -> None:
with Image.open("Tests/images/hopper.gif") as im:
assert im.getxmp() == {}

def test_getxmp_padded(self) -> None:
im = Image.new("RGB", (1, 1))
im.info["xmp"] = (
b'<?xpacket begin="\xef\xbb\xbf" id="W5M0MpCehiHzreSzNTczkc9d"?>\n'
b'<x:xmpmeta xmlns:x="adobe:ns:meta/" />\n<?xpacket end="w"?>\x00\x00'
)
if ElementTree is None:
with pytest.warns(
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}
else:
assert im.getxmp() == {"xmpmeta": None}

@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
def test_zero_tobytes(self, size: tuple[int, int]) -> None:
im = Image.new("RGB", size)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ def get_value(element):
return {}
if "xmp" not in self.info:
return {}
root = ElementTree.fromstring(self.info["xmp"])
root = ElementTree.fromstring(self.info["xmp"].rstrip(b"\x00"))
return {get_name(root.tag): get_value(root)}

def getexif(self) -> Exif:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def APP(self, marker):
self.info["exif"] = s
self._exif_offset = self.fp.tell() - n + 6
elif marker == 0xFFE1 and s[:29] == b"http://ns.adobe.com/xap/1.0/\x00":
self.info["xmp"] = s.split(b"\x00")[1]
self.info["xmp"] = s.split(b"\x00", 1)[1]
elif marker == 0xFFE2 and s[:5] == b"FPXR\0":
# extract FlashPix information (incomplete)
self.info["flashpix"] = s # FIXME: value will change
Expand Down

0 comments on commit 6a44854

Please sign in to comment.