Search code examples
pythonpython-imaging-library

PIL - Images not rotating


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?


Solution

  • 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)