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

ImageOps.exif_transpose raises a KeyError when removing the EXIF orientation tag #5580

Closed
Lucidiot opened this issue Jul 5, 2021 · 4 comments · Fixed by #5584
Closed

ImageOps.exif_transpose raises a KeyError when removing the EXIF orientation tag #5580

Lucidiot opened this issue Jul 5, 2021 · 4 comments · Fixed by #5584

Comments

@Lucidiot
Copy link

Lucidiot commented Jul 5, 2021

What did you do?

I used ImageOps.exif_transpose on an image with an EXIF orientation tag that should cause a rotation.

What did you expect to happen?

Get a rotated Image with the EXIF orientation removed.

What actually happened?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    ImageOps.exif_transpose(img)
  File "/usr/lib/python3.8/site-packages/PIL/ImageOps.py", line 591, in exif_transpose
    del transposed_exif[0x0112]
  File "/usr/lib/python3.8/site-packages/PIL/Image.py", line 3598, in __delitem__
    del self._data[tag]
KeyError: 274

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 20.04.2
  • Python: 3.8.10
  • Pillow: 8.3.0

Steps to reproduce

from PIL import Image, ImageOps
img = Image.new('L', (200, 200))
# Setting the orientation to rotate 270° clockwise
img.getexif()[0x0112] = 6
# this will cause a KeyError
transposed = ImageOps.exif_transpose(img)

Possible fix

From some quick reading, I believe this line is the issue. The EXIF tags in a newly transposed image are empty:

>>> dict(img.transpose(Image.ROTATE_270).getexif())
{}

But the method attempts to remove the orientation tag from this empty dictionary. It seems the change was made to avoid editing the EXIF tags of the original image (#5546), and using copy.copy would probably fix this:

from copy import copy
transposed_exif = copy(exif)
del transposed_exif[0x0112]
print(exif[0x0112])  # it's still there, unmodified
@radarhere
Copy link
Member

Thanks for the detailed analysis. I'll create PR to resolve this shortly, but I would also like to ask - I'm guessing you found this bug by opening an actual image file, right? If you don't want to attach that image, could you let us know the image format and the info dictionary? I'm interested in how the EXIF data was stored.

@radarhere
Copy link
Member

I've created #5584 to resolve this.

@Lucidiot
Copy link
Author

Lucidiot commented Jul 6, 2021

I had this issue on a TIFF image, here it is: image.tiff.zip

The info dict has {'compression': 'group4', 'dpi': (300, 300)}.

Thanks for the quick fix!

@radarhere
Copy link
Member

Pillow 8.3.1 has now been released with a fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants