Skip to content

Commit 67ffa0d

Browse files
committed
Parse XMP tag bytes without decoding to string
1 parent 5a04b95 commit 67ffa0d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Tests/test_image.py

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

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

src/PIL/Image.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,10 +1538,11 @@ def getexif(self) -> Exif:
15381538
# XMP tags
15391539
if ExifTags.Base.Orientation not in self._exif:
15401540
xmp_tags = self.info.get("XML:com.adobe.xmp")
1541+
pattern: str | bytes = r'tiff:Orientation(="|>)([0-9])'
15411542
if not xmp_tags and (xmp_tags := self.info.get("xmp")):
1542-
xmp_tags = xmp_tags.decode("utf-8")
1543+
pattern = rb'tiff:Orientation(="|>)([0-9])'
15431544
if xmp_tags:
1544-
match = re.search(r'tiff:Orientation(="|>)([0-9])', xmp_tags)
1545+
match = re.search(pattern, xmp_tags)
15451546
if match:
15461547
self._exif[ExifTags.Base.Orientation] = int(match[2])
15471548

0 commit comments

Comments
 (0)