I'm attempting to take large (huge) images (from a digital camera), and convert them into something that I can display on the web. This seems straightforward, and probably should be. However, when I attempt to use PIL to create thumbnail versions, if my source image is taller than it is wide, the resulting image is rotated 90 degrees, such that the top of the source image is on the left of the resulting image. If the source image is wider than it is tall, the resulting image is the correct (original) orientation. Could it have to do with the 2-tuple I send in as the size? I'm using thumbnail, because it appears it was meant to preserve the aspect ratio. Or am I just being completely blind, and doing something dumb? The size tuple is 1000,1000 because I want the longest side to be shrunk to 1000 pixels, while keeping AR preserved.
Code seems simple
img = Image.open(filename) img.thumbnail((1000,1000), Image.ANTIALIAS) img.save(output_fname, "JPEG")
Thanks in advance for any help.
The feature that's causing your photos to rotate and remain oriented the same way at all times is called Auto-Rotate, and it's actually a very good feature because it ensures that you'll be able to read whatever text is on the screen regardless of how you choose to hold your phone.
Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it.
Photos taken on smartphones, tablets and some cameras can look great on your device but appear upside down or sideways when uploaded to a post or page because the device stores the image's orientation in the EXIF metadata and not all software is able to read the metadata.
PIL.Image.Image.rotate() method – This method is used to rotate a given image to the given number of degrees counter clockwise around its centre. Parameters: image_object: It is the real image which is to be rotated. angle: In degrees counter clockwise.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With