diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py
index d0d394aa9..73046eb5f 100644
--- a/Tests/test_file_tiff.py
+++ b/Tests/test_file_tiff.py
@@ -14,6 +14,7 @@ from PIL import (
ImageFile,
JpegImagePlugin,
TiffImagePlugin,
+ TiffTags,
UnidentifiedImageError,
)
from PIL.TiffImagePlugin import RESOLUTION_UNIT, X_RESOLUTION, Y_RESOLUTION
@@ -900,6 +901,29 @@ class TestFileTiff:
assert description[0]["format"] == "image/tiff"
assert description[3]["BitsPerSample"]["Seq"]["li"] == ["8", "8", "8"]
+ def test_getxmp_undefined(self, tmp_path: Path) -> None:
+ tmpfile = tmp_path / "temp.tif"
+ im = Image.new("L", (1, 1))
+ ifd = TiffImagePlugin.ImageFileDirectory_v2()
+ ifd.tagtype[700] = TiffTags.UNDEFINED
+ with Image.open("Tests/images/lab.tif") as im_xmp:
+ ifd[700] = im_xmp.info["xmp"]
+ im.save(tmpfile, tiffinfo=ifd)
+
+ with Image.open(tmpfile) as im_reloaded:
+ if ElementTree is None:
+ with pytest.warns(
+ UserWarning,
+ match="XMP data cannot be read without defusedxml dependency",
+ ):
+ assert im_reloaded.getxmp() == {}
+ else:
+ assert "xmp" in im_reloaded.info
+ xmp = im_reloaded.getxmp()
+
+ description = xmp["xmpmeta"]["RDF"]["Description"]
+ assert description[0]["format"] == "image/tiff"
+
def test_get_photoshop_blocks(self) -> None:
with Image.open("Tests/images/lab.tif") as im:
assert isinstance(im, TiffImagePlugin.TiffImageFile)
diff --git a/Tests/test_image.py b/Tests/test_image.py
index 512a52433..b018b4309 100644
--- a/Tests/test_image.py
+++ b/Tests/test_image.py
@@ -995,7 +995,7 @@ class TestImage:
im = Image.new("RGB", (1, 1))
im.info["xmp"] = (
b'\n'
- b'\n\x00\x00'
+ b'\n\x00\x00 '
)
if ElementTree is None:
with pytest.warns(
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
index 216022565..7e9540e48 100644
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -1511,7 +1511,7 @@ class Image:
return {}
if "xmp" not in self.info:
return {}
- root = ElementTree.fromstring(self.info["xmp"].rstrip(b"\x00"))
+ root = ElementTree.fromstring(self.info["xmp"].rstrip(b"\x00 "))
return {get_name(root.tag): get_value(root)}
def getexif(self) -> Exif:
diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
index 5cbac0c26..946fbd531 100644
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -1260,7 +1260,10 @@ class TiffImageFile(ImageFile.ImageFile):
self.fp.seek(self._frame_pos[frame])
self.tag_v2.load(self.fp)
if XMP in self.tag_v2:
- self.info["xmp"] = self.tag_v2[XMP]
+ xmp = self.tag_v2[XMP]
+ if isinstance(xmp, tuple) and len(xmp) == 1:
+ xmp = xmp[0]
+ self.info["xmp"] = xmp
elif "xmp" in self.info:
del self.info["xmp"]
self._reload_exif()