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