Skip to content

Commit 304cf48

Browse files
committed
Do not presume "xmp" info simply because "XML:com.adobe.xmp" info exists
1 parent a90a9d5 commit 304cf48

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Tests/test_imageops.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,16 @@ def check(orientation_im: Image.Image) -> None:
432432
assert 0x0112 not in transposed_im.getexif()
433433

434434

435+
def test_exif_transpose_xml_without_xmp() -> None:
436+
with Image.open("Tests/images/xmp_tags_orientation.png") as im:
437+
assert im.getexif()[0x0112] == 3
438+
assert "XML:com.adobe.xmp" in im.info
439+
440+
del im.info["xmp"]
441+
transposed_im = ImageOps.exif_transpose(im)
442+
assert 0x0112 not in transposed_im.getexif()
443+
444+
435445
def test_exif_transpose_in_place() -> None:
436446
with Image.open("Tests/images/orientation_rectangle.jpg") as im:
437447
assert im.size == (2, 1)

src/PIL/ImageOps.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -709,17 +709,18 @@ def exif_transpose(image: Image.Image, *, in_place: bool = False) -> Image.Image
709709
exif_image.info["exif"] = exif.tobytes()
710710
elif "Raw profile type exif" in exif_image.info:
711711
exif_image.info["Raw profile type exif"] = exif.tobytes().hex()
712-
elif "XML:com.adobe.xmp" in exif_image.info:
713-
for pattern in (
714-
r'tiff:Orientation="([0-9])"',
715-
r"<tiff:Orientation>([0-9])</tiff:Orientation>",
716-
):
717-
exif_image.info["XML:com.adobe.xmp"] = re.sub(
718-
pattern, "", exif_image.info["XML:com.adobe.xmp"]
719-
)
720-
exif_image.info["xmp"] = re.sub(
721-
pattern.encode(), b"", exif_image.info["xmp"]
722-
)
712+
for key in ("XML:com.adobe.xmp", "xmp"):
713+
if key in exif_image.info:
714+
for pattern in (
715+
r'tiff:Orientation="([0-9])"',
716+
r"<tiff:Orientation>([0-9])</tiff:Orientation>",
717+
):
718+
value = exif_image.info[key]
719+
exif_image.info[key] = (
720+
re.sub(pattern, "", value)
721+
if isinstance(value, str)
722+
else re.sub(pattern.encode(), b"", value)
723+
)
723724
if not in_place:
724725
return transposed_image
725726
elif not in_place:

0 commit comments

Comments
 (0)