Skip to content

Commit 646885e

Browse files
authored
Parse XMP tag bytes without decoding to string (#8960)
Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
1 parent b844007 commit 646885e

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

Tests/test_image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,11 @@ def test_exif_hide_offsets(self) -> None:
974974
assert tag not in exif.get_ifd(0x8769)
975975
assert exif.get_ifd(0xA005)
976976

977+
def test_exif_from_xmp_bytes(self) -> None:
978+
im = Image.new("RGB", (1, 1))
979+
im.info["xmp"] = b'\xff tiff:Orientation="2"'
980+
assert im.getexif()[274] == 2
981+
977982
def test_empty_xmp(self) -> None:
978983
with Image.open("Tests/images/hopper.gif") as im:
979984
if ElementTree is None:

src/PIL/Image.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,10 +1542,11 @@ def getexif(self) -> Exif:
15421542
# XMP tags
15431543
if ExifTags.Base.Orientation not in self._exif:
15441544
xmp_tags = self.info.get("XML:com.adobe.xmp")
1545+
pattern: str | bytes = r'tiff:Orientation(="|>)([0-9])'
15451546
if not xmp_tags and (xmp_tags := self.info.get("xmp")):
1546-
xmp_tags = xmp_tags.decode("utf-8")
1547+
pattern = rb'tiff:Orientation(="|>)([0-9])'
15471548
if xmp_tags:
1548-
match = re.search(r'tiff:Orientation(="|>)([0-9])', xmp_tags)
1549+
match = re.search(pattern, xmp_tags)
15491550
if match:
15501551
self._exif[ExifTags.Base.Orientation] = int(match[2])
15511552

0 commit comments

Comments
 (0)