diff --git a/Tests/images/xmp_tags_orientation_exiftool.png b/Tests/images/xmp_tags_orientation_exiftool.png
index fb30199c86a..e366f0b9cc8 100644
Binary files a/Tests/images/xmp_tags_orientation_exiftool.png and b/Tests/images/xmp_tags_orientation_exiftool.png differ
diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py
index 95b49596e79..bd5f44e5008 100644
--- a/Tests/test_imageops.py
+++ b/Tests/test_imageops.py
@@ -345,18 +345,12 @@ def check(orientation_im):
check(orientation_im)
# Orientation from "XML:com.adobe.xmp" info key
- with Image.open("Tests/images/xmp_tags_orientation.png") as im:
- assert im.getexif()[0x0112] == 3
-
- transposed_im = ImageOps.exif_transpose(im)
- assert 0x0112 not in transposed_im.getexif()
+ for suffix in ("", "_exiftool"):
+ with Image.open("Tests/images/xmp_tags_orientation" + suffix + ".png") as im:
+ assert im.getexif()[0x0112] == 3
- # Orientation from "XML:com.adobe.xmp" info key (from exiftool)
- with Image.open("Tests/images/xmp_tags_orientation_exiftool.png") as im:
- assert im.getexif()[0x0112] == 8
-
- transposed_im = ImageOps.exif_transpose(im)
- assert 0x0112 not in transposed_im.getexif()
+ transposed_im = ImageOps.exif_transpose(im)
+ assert 0x0112 not in transposed_im.getexif()
# Orientation from "Raw profile type exif" info key
# This test image has been manually hexedited from exif_imagemagick.png
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
index 816ea94db8d..4eb2dead655 100644
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -1404,15 +1404,9 @@ def getexif(self):
if 0x0112 not in self._exif:
xmp_tags = self.info.get("XML:com.adobe.xmp")
if xmp_tags:
- match = re.search(r'tiff:Orientation="([0-9])"', xmp_tags)
+ match = re.search(r'tiff:Orientation(="|>)([0-9])', xmp_tags)
if match:
- self._exif[0x0112] = int(match[1])
- else:
- match = re.search(
- r"([0-9])", xmp_tags
- )
- if match:
- self._exif[0x0112] = int(match[1])
+ self._exif[0x0112] = int(match[2])
return self._exif
diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py
index b26b1858b93..48b41d87fda 100644
--- a/src/PIL/ImageOps.py
+++ b/src/PIL/ImageOps.py
@@ -601,15 +601,12 @@ def exif_transpose(image):
"Raw profile type exif"
] = transposed_exif.tobytes().hex()
elif "XML:com.adobe.xmp" in transposed_image.info:
- transposed_image.info["XML:com.adobe.xmp"] = re.sub(
+ for pattern in (
r'tiff:Orientation="([0-9])"',
- "",
- transposed_image.info["XML:com.adobe.xmp"],
- )
- transposed_image.info["XML:com.adobe.xmp"] = re.sub(
r"([0-9])",
- "",
- transposed_image.info["XML:com.adobe.xmp"],
- )
+ ):
+ transposed_image.info["XML:com.adobe.xmp"] = re.sub(
+ pattern, "", transposed_image.info["XML:com.adobe.xmp"]
+ )
return transposed_image
return image.copy()