diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 57151bc9670..9479bc550e2 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -1,8 +1,18 @@ from __future__ import annotations +from io import BytesIO + import pytest -from PIL import ExifTags, Image, ImageDraw, ImageOps, ImageStat, features +from PIL import ( + ExifTags, + Image, + ImageDraw, + ImageOps, + ImageStat, + PngImagePlugin, + features, +) from .helper import ( assert_image_equal, @@ -589,6 +599,47 @@ def test_exif_transpose_xml_without_xmp() -> None: assert 0x0112 not in transposed_im.getexif() +@pytest.mark.parametrize("format", ("JPEG", "PNG")) +@pytest.mark.parametrize( + "attrs, element", + ( + (" tiff:Orientation = '6'", ""), + ("", "\n 6 "), + ), +) +def test_exif_transpose_xmp_xml_syntax(format: str, attrs: str, element: str) -> None: + xmp = ( + '' + f'{element}' + "" + ) + im = Image.new("RGB", (8, 4), "red") + output = BytesIO() + if format == "PNG": + info = PngImagePlugin.PngInfo() + info.add_itxt("XML:com.adobe.xmp", xmp) + im.save(output, format, pnginfo=info) + else: + im.save(output, format, xmp=xmp.encode()) + + with Image.open(output) as reopened: + assert reopened.getexif()[ExifTags.Base.Orientation] == 6 + transposed = ImageOps.exif_transpose(reopened) + assert transposed.size == (4, 8) + assert ExifTags.Base.Orientation not in transposed.copy().getexif() + assert b'tiff:Make="Pillow"' in transposed.info["xmp"] + assert b"tiff:Orientation" not in transposed.info["xmp"] + + +def test_exif_transpose_xmp_does_not_read_partial_orientation() -> None: + im = Image.new("RGB", (8, 4)) + im.info["xmp"] = b'' + + assert ExifTags.Base.Orientation not in im.getexif() + assert ImageOps.exif_transpose(im).size == im.size + + def test_exif_transpose_raw_profile() -> None: exif = Image.Exif() exif[0x0112] = 3 diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index ad1ff23a499..860aa1d75f7 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -93,6 +93,13 @@ Two new image processing operations are available in ImageOps. Other changes ============= +Reading XMP orientation +^^^^^^^^^^^^^^^^^^^^^^^ + +:py:meth:`~PIL.Image.Image.getexif` and :py:func:`~PIL.ImageOps.exif_transpose` +now recognize TIFF orientation in XMP attributes with single quotes or extra +whitespace and in XMP elements with whitespace around the value. + PCX and ZIP decoder row sizes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a54bd282d6c..257b0318ea8 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -71,6 +71,13 @@ from typing import Any, Literal, Self +# XMP can store TIFF orientation as either an XML attribute or an element. +_XMP_ORIENTATION = ( + r"""(?<=\s)tiff:Orientation\s*=\s*(["'])\s*([0-9])\s*\1""" + r"|\s*([0-9])\s*" +) + + class DecompressionBombWarning(RuntimeWarning): pass @@ -1702,14 +1709,16 @@ def getexif(self) -> Exif: # XMP tags if ExifTags.Base.Orientation not in self._exif: - xmp_tags = self.info.get("XML:com.adobe.xmp") - pattern: str | bytes = r'tiff:Orientation(="|>)([0-9])' - if not xmp_tags and (xmp_tags := self.info.get("xmp")): - pattern = rb'tiff:Orientation(="|>)([0-9])' - if xmp_tags: + xmp_tags = self.info.get("XML:com.adobe.xmp") or self.info.get("xmp") + if isinstance(xmp_tags, (str, bytes)): + pattern: str | bytes = ( + _XMP_ORIENTATION + if isinstance(xmp_tags, str) + else _XMP_ORIENTATION.encode() + ) match = re.search(pattern, xmp_tags) if match: - self._exif[ExifTags.Base.Orientation] = int(match[2]) + self._exif[ExifTags.Base.Orientation] = int(match[2] or match[3]) return self._exif diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index d3483b58bac..71c8c630a2a 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -885,17 +885,13 @@ def exif_transpose(image: Image.Image, *, in_place: bool = False) -> Image.Image f"\nexif\n{len(data):8}\n{data.hex()}\n" ) for key in ("XML:com.adobe.xmp", "xmp"): - if key in exif_image.info: - for pattern in ( - r'tiff:Orientation="([0-9])"', - r"([0-9])", - ): - value = exif_image.info[key] - exif_image.info[key] = ( - re.sub(pattern, "", value) - if isinstance(value, str) - else re.sub(pattern.encode(), b"", value) - ) + value = exif_image.info.get(key) + if isinstance(value, str): + exif_image.info[key] = re.sub(Image._XMP_ORIENTATION, "", value) + elif isinstance(value, bytes): + exif_image.info[key] = re.sub( + Image._XMP_ORIENTATION.encode(), b"", value + ) if not in_place: return transposed_image elif not in_place: