Unexpected rotation on saving a JPEG with orientation exif.

Issue #33 new
Kit Sunde
created an issue

If we do the following on the attached image:

from PIL import Image
image = Image.open("photo-1.JPG")
image.save('test.jpeg')

The new image will have a different orientation because PIL doesn't maintain orientation exif data.

Instead we have to resort too:

from PIL import Image
im = Image.open("photo-1.JPG")
if hasattr(image, '_getexif'):
    orientation = 0x0112
    exif = image._getexif()
    if exif is not None:
        orientation = exif[orientation]
        rotations = {
            3: Image.ROTATE_180,
            6: Image.ROTATE_270,
            8: Image.ROTATE_90
        }
        image = image.transpose(rotations[orientation])
image.save('test.jpeg')

Which is quite unexpected when it seemingly randomly happens, and requires insight into how JPEG work to be able to remedy. It seems like there are three ways to solve this:

  1. PIL doesn't strip exif data that would be unchanged by PIL manipulation.
  2. Keep the orientation exif.
  3. Internally use the code above to work around the issue.

I would prefer 1., but 3. may be easier.

Comments (3)

  1. Kit Sunde reporter

    Heh it seems like my attachment thumbnail appear rotated in chrome as well, possibly because bitbucket uses PIL as well. If you click the thumbnail you'll get an image with the correct orientation.

  2. Log in to comment