Merge pull request #8173 from radarhere/xmp_imageops

This commit is contained in:
Hugo van Kemenade 2024-06-27 12:40:07 +03:00 committed by GitHub
commit 6a2b8e7236
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 11 deletions

View File

@ -432,6 +432,16 @@ def test_exif_transpose() -> None:
assert 0x0112 not in transposed_im.getexif() assert 0x0112 not in transposed_im.getexif()
def test_exif_transpose_xml_without_xmp() -> None:
with Image.open("Tests/images/xmp_tags_orientation.png") as im:
assert im.getexif()[0x0112] == 3
assert "XML:com.adobe.xmp" in im.info
del im.info["xmp"]
transposed_im = ImageOps.exif_transpose(im)
assert 0x0112 not in transposed_im.getexif()
def test_exif_transpose_in_place() -> None: def test_exif_transpose_in_place() -> None:
with Image.open("Tests/images/orientation_rectangle.jpg") as im: with Image.open("Tests/images/orientation_rectangle.jpg") as im:
assert im.size == (2, 1) assert im.size == (2, 1)

View File

@ -709,17 +709,18 @@ def exif_transpose(image: Image.Image, *, in_place: bool = False) -> Image.Image
exif_image.info["exif"] = exif.tobytes() exif_image.info["exif"] = exif.tobytes()
elif "Raw profile type exif" in exif_image.info: elif "Raw profile type exif" in exif_image.info:
exif_image.info["Raw profile type exif"] = exif.tobytes().hex() exif_image.info["Raw profile type exif"] = exif.tobytes().hex()
elif "XML:com.adobe.xmp" in exif_image.info: for key in ("XML:com.adobe.xmp", "xmp"):
for pattern in ( if key in exif_image.info:
r'tiff:Orientation="([0-9])"', for pattern in (
r"<tiff:Orientation>([0-9])</tiff:Orientation>", r'tiff:Orientation="([0-9])"',
): r"<tiff:Orientation>([0-9])</tiff:Orientation>",
exif_image.info["XML:com.adobe.xmp"] = re.sub( ):
pattern, "", exif_image.info["XML:com.adobe.xmp"] value = exif_image.info[key]
) exif_image.info[key] = (
exif_image.info["xmp"] = re.sub( re.sub(pattern, "", value)
pattern.encode(), b"", exif_image.info["xmp"] if isinstance(value, str)
) else re.sub(pattern.encode(), b"", value)
)
if not in_place: if not in_place:
return transposed_image return transposed_image
elif not in_place: elif not in_place: