Skip to content

Commit 35d536e

Browse files
committed
Parse XMP tag bytes without decoding to string
1 parent 3c71559 commit 35d536e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Tests/test_image.py

+5
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,11 @@ def test_exif_hide_offsets(self) -> None:
975975
assert tag not in exif.get_ifd(0x8769)
976976
assert exif.get_ifd(0xA005)
977977

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

src/PIL/Image.py

+3-2
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)