Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Resizing the portrait-oriented images rotates it to 90° degrees.
-
Hi. I'm trying to write a script to resize the image to 1200 pixels wide.
Everything seems to be working fine apart from the fact that all portrait-oriented photos are rotated 90° degrees.The images are displayed correctly (in portrait orientation) in my camera roll.
But when I'm running the script they are rotated 90° degrees to landscape mode.
What am I doing wrong here?
Here is the script I'm using.
from PIL import Image import photos import console image = photos.pick_image() def customSize(image): w, h = image.size print 'Original image size: '+str(w)+' × '+str(h)+' pixels'+'\n' if w > 1200: wsize = 1200/float(w) hsize = int(float(h)*float(wsize)) image = image.resize((1200, hsize), Image.ANTIALIAS) print 'Modified image size: 1200 × '+str(hsize)+' pixels' else: print 'Image is too small to be resampled. Width is less than 1200 pixels' return image image = customSize(image) image.show() saveit = photos.save_image(image) print 'Done!'
-
import photos, PIL photos.pick_image().show()
...will exhibit the same behavior with portrait images.
The problem here is that the documentation says that the
Image.show()
"method is mainly intended for debugging purposes". This means that you should not use it as a "production" user interface. Consider using the ui or scene modules for end user display of images instead of usingImage.show()
.Also, consider using
format()
to put values into strings:print('Original image size: {} x {} pixels\n'.format(w, h)) # or even print('Original image size: {} x {} pixels\n'.format(*image.size))
-
The problem is basically that
pick_image
will return the original image data by default. The camera usually stores portrait photos as landscape images with additional metadata that specifies the orientation.You can pass
original=False
topick_image()
to get the image that you see in the Photos app, though this may also result in a downscaled version of the image (since you're resizing it anyway, this shouldn't actually matter in this case). Alternatively, you could look at the image's metadata to figure out the orientation yourself, but in this case, I would really recommend tryingoriginal=False
first. -
Thanks, omz. <code>original=False</code> helped. The only downside is that the image is already scaled to less than 1200 pixels (as you've said).
It still will work for me though.
I will take a look metadata option when have a little bit more free time :)Thanks again.
-
Thanks for the comment, ccc.
I will take a look at ui scene module as well.