3

I'm curious as to why my image is not rotating, it ends up in the same position every time.

img = Image.open(r'C:\Users\Brett\Downloads\testing.jpg')
exif_data = {
    TAGS[k]: v
    for k, v in img._getexif().items()
    if k in TAGS
}
print(exif_data['Orientation'])

That outputs a '6'

No matter how many degrees I tell the image to rotate it ends up in the same position.

if exif_data['Orientation'] == 6:
    img.rotate(90)

or

if exif_data['Orientation'] == 6:
    img.rotate(270) 

or

if exif_data['Orientation'] == 6:
    img.rotate(180)

I always end up with an image rotated 90 degrees counter-clockwise. Am I doing something obviously wrong?

1
  • 2
    Did you save the rotated image? May 14, 2017 at 16:49

2 Answers 2

6

From the (DOCS)

Image.rotate(angle, resample=0, expand=0, center=None, translate=None)

Returns a rotated copy of this image. This method returns a copy of this image, rotated the given number of degrees counter clockwise around its centre.

The image is not rotated in place. You need to store the image returned from rotate(). Maybe something like:

if exif_data['Orientation'] == 6:
    new_image = img.rotate(180)
2
  • Thank you. Makes sense for it to be that way.
    – BrettJ
    May 14, 2017 at 22:40
  • 2
    Yes but, the verb rotate sounds like it might be in place. Maybe they should have called it rotated?
    – Stephen Rauch
    May 14, 2017 at 22:41
1

In addition to the suggested answer above you could also just do the following.

if exif_data['Orientation'] == 6:
    img = img.rotate(90)

or

if exif_data['Orientation'] == 6:
    img = img.rotate(270)

or

if exif_data['Orientation'] == 6:
    img = img.rotate(180)

Additionally as mentioned above you may want to set the expand parameter, like the following to ensure that you don't run out of canvas for the rotated image.

if exif_data['Orientation'] == 6:
    img.rotate(180,expand=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.