Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse orientation from XMP tag contents #6463

Merged
merged 7 commits into from Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/xmp_tags_orientation_exiftool.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions Tests/test_imageops.py
Expand Up @@ -345,11 +345,15 @@ 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
for suffix in ("", "_exiftool"):
with Image.open("Tests/images/xmp_tags_orientation" + suffix + ".png") as im:
assert im.getexif()[0x0112] == 3

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()

transposed_im._reload_exif()
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
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/Image.py
Expand Up @@ -1404,9 +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])
self._exif[0x0112] = int(match[2])

return self._exif

Expand Down
10 changes: 6 additions & 4 deletions src/PIL/ImageOps.py
Expand Up @@ -601,10 +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"],
)
r"<tiff:Orientation>([0-9])</tiff:Orientation>",
bigcat88 marked this conversation as resolved.
Show resolved Hide resolved
):
transposed_image.info["XML:com.adobe.xmp"] = re.sub(
pattern, "", transposed_image.info["XML:com.adobe.xmp"]
)
return transposed_image
return image.copy()