PIL thumbnail is rotating my image?

I agree with almost everything as answered by "unutbu" and Ignacio Vazquez-Abrams, however...

EXIF Orientation flag can have a value between 1 and 8 depending on how the camera was held.

Portrait photo can be taken with top of the camera on the left, or right edge, landscape photo could be taken upside down.

Here is code that takes this into account (Tested with DSLR Nikon D80)

    import Image, ExifTags

    try :
        image=Image.open(os.path.join(path, fileName))
        for orientation in ExifTags.TAGS.keys() : 
            if ExifTags.TAGS[orientation]=='Orientation' : break 
        exif=dict(image._getexif().items())

        if   exif[orientation] == 3 : 
            image=image.rotate(180, expand=True)
        elif exif[orientation] == 6 : 
            image=image.rotate(270, expand=True)
        elif exif[orientation] == 8 : 
            image=image.rotate(90, expand=True)

        image.thumbnail((THUMB_WIDTH , THUMB_HIGHT), Image.ANTIALIAS)
        image.save(os.path.join(path,fileName))

    except:
        traceback.print_exc()

Just use PIL.ImageOps.exif_transpose from Pillow.

Unlike every single function proposed in answers to this question, including my original one, it takes care to remove the orientation field from EXIF (as the image is no longer oriented in a strange way) and also to ensure the return value is a brand new Image object so changes to it can’t affect the original one.