Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion Tests/test_imageops.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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'", ""),
("", "<tiff:Orientation>\n 6 </tiff:Orientation>"),
),
)
def test_exif_transpose_xmp_xml_syntax(format: str, attrs: str, element: str) -> None:
xmp = (
'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'
' xmlns:tiff="http://ns.adobe.com/tiff/1.0/">'
f'<rdf:Description tiff:Make="Pillow"{attrs}>{element}</rdf:Description>'
"</rdf:RDF>"
)
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'<rdf:Description tiff:Orientation="60" />'

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
Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
21 changes: 15 additions & 6 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"|<tiff:Orientation\s*>\s*([0-9])\s*</tiff:Orientation\s*>"
)


class DecompressionBombWarning(RuntimeWarning):
pass

Expand Down Expand Up @@ -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

Expand Down
18 changes: 7 additions & 11 deletions src/PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<tiff:Orientation>([0-9])</tiff:Orientation>",
):
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:
Expand Down