Python Image Library: Image is turned by 90 degree?

Question:

I have an image on my computer which has the dimensions width=1932 and height=2576. It was made with a smartphone and uses the "jpeg"-format.

If I open the image with any tool I like it is shown correctly.

I tried to open it with python:

from PIL import Image
img_in = Image.open(input_image_path)

Unfortunately in python it always has the dimensions width=2576 and height=1932. It this would always happen, i could fix it, but it seems only to happen for some of my images. The images are always rotated clockwise.

Do I use the the open-function wrong or how could I fix this?


A solution I found is to follow this code: https://stackoverflow.com/a/11543365/916672

Asked By: Kevin Meier

||

Answers:

When you do a portrait picture with your smartphone and a landscape picture, both will be done with the same coordinate system, that is, relative to the orientation of the imaging sensor in your phone.

However, the phone’s gyroscope detects the rotation of the device, so it “knows” whether it is a portrait or landscape picture. This information is saved in the JPEG’s metadata.

Software to display images typically evaluates this information to rotate the picture accordingly. In image editing software, you should typically be asked if you want to apply the rotation to the picture (e.g. The Gimp does this).

However, image processing libraries typically completely ignore this information. They only access the image pixels, and they access them in the order they are stored. As you can see there is nothing wrong with python imaging, however it would be interested if there is an interface to also read this information, so you can deal with it accordingly.

Answered By: ypnos