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

Added in_place argument to ImageOps.exif_transpose() #7092

Merged
merged 7 commits into from Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/orientation_rectangle.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions Tests/test_imageops.py
Expand Up @@ -404,6 +404,18 @@ def check(orientation_im):
assert 0x0112 not in transposed_im.getexif()


def test_exif_transpose_inplace():
hugovk marked this conversation as resolved.
Show resolved Hide resolved
with Image.open("Tests/images/orientation_rectangle.jpg") as im:
assert im.size == (2, 1)
assert im.getexif()[0x0112] == 8
hugovk marked this conversation as resolved.
Show resolved Hide resolved
expected = im.rotate(90, expand=True)

ImageOps.exif_transpose(im, inPlace=True)
hugovk marked this conversation as resolved.
Show resolved Hide resolved
assert im.size == (1, 2)
assert 0x0112 not in im.getexif()
assert_image_equal(im, expected)


def test_autocontrast_cutoff():
# Test the cutoff argument of autocontrast
with Image.open("Tests/images/bw_gradient.png") as img:
Expand Down
53 changes: 31 additions & 22 deletions src/PIL/ImageOps.py
Expand Up @@ -576,19 +576,20 @@
return _lut(image, lut)


def exif_transpose(image):
def exif_transpose(image, inPlace=False):
hugovk marked this conversation as resolved.
Show resolved Hide resolved
"""
If an image has an EXIF Orientation tag, other than 1, return a new image
that is transposed accordingly. The new image will have the orientation
data removed.

Otherwise, return a copy of the image.
If an image has an EXIF Orientation tag, other than 1, transpose the image
accordingly, and remove the orientation data.

:param image: The image to transpose.
:return: An image.
"""
exif = image.getexif()
orientation = exif.get(0x0112)
:param inPlace: Boolean.
hugovk marked this conversation as resolved.
Show resolved Hide resolved
If ``True``, the original image is modified in-place, and ``None`` is returned.
If ``False`` (default), a new :py:class:`~PIL.Image.Image` object is returned
with the transposition applied. If there is no transposition, a copy of the
image will be returned.
"""
image_exif = image.getexif()
orientation = image_exif.get(0x0112)
method = {
2: Image.Transpose.FLIP_LEFT_RIGHT,
3: Image.Transpose.ROTATE_180,
Expand All @@ -600,22 +601,30 @@
}.get(orientation)
if method is not None:
transposed_image = image.transpose(method)
transposed_exif = transposed_image.getexif()
if 0x0112 in transposed_exif:
del transposed_exif[0x0112]
if "exif" in transposed_image.info:
transposed_image.info["exif"] = transposed_exif.tobytes()
elif "Raw profile type exif" in transposed_image.info:
transposed_image.info[
"Raw profile type exif"
] = transposed_exif.tobytes().hex()
elif "XML:com.adobe.xmp" in transposed_image.info:
if inPlace:
image.im = transposed_image.im
image.pyaccess = None
image._size = transposed_image._size
exif_image = image if inPlace else transposed_image
hugovk marked this conversation as resolved.
Show resolved Hide resolved

exif = exif_image.getexif()
if 0x0112 in exif:
del exif[0x0112]
if "exif" in exif_image.info:
exif_image.info["exif"] = exif.tobytes()
elif "Raw profile type exif" in exif_image.info:
exif_image.info["Raw profile type exif"] = exif.tobytes().hex()
elif "XML:com.adobe.xmp" in exif_image.info:
for pattern in (
r'tiff:Orientation="([0-9])"',
r"<tiff:Orientation>([0-9])</tiff:Orientation>",
):
transposed_image.info["XML:com.adobe.xmp"] = re.sub(
pattern, "", transposed_image.info["XML:com.adobe.xmp"]
exif_image.info["XML:com.adobe.xmp"] = re.sub(
pattern, "", exif_image.info["XML:com.adobe.xmp"]
)
if inPlace:
return
return transposed_image
if inPlace:
hugovk marked this conversation as resolved.
Show resolved Hide resolved
return

Check warning on line 629 in src/PIL/ImageOps.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageOps.py#L629

Added line #L629 was not covered by tests
return image.copy()