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.
Resize Image with Pillow and the right orientation
-
Hi,
i am trying to resize an Image from my photo library and upload it. But there is a problem with the orientation of the picture. How can i first rotate the image in the right orientation and then resize it?
def uploadimage(sender): mywidth = 1600 for file in vdel.items: name = file['name'] asset = photos.get_asset_with_local_id(file['id']) data = asset.get_image() wpercent = (mywidth/float(data.size[0])) hsize = int((float(data.size[1])*float(wpercent))) data = data.resize((mywidth,hsize)) imgByteArr = io.BytesIO() data.save(imgByteArr, format='JPEG') imgByteArr = imgByteArr.getvalue() uploadfile = {'file': (name, imgByteArr)}
The function is working but i have to fix the orientation.
BR kami
-
@kami said
problem with the orientation
What is the problem?
Do you mean that you turned your camera before taking the photo?Kind of problems
Flag is orientation flag in exifs of the photo
Anyway, your "data" is a PIL.Image and can be rotated (before resizing) with data.rotate(...), see Pythonista doc.
And use this kind of code to get this flag
import photos assets = photos.get_assets() asset = photos.pick_asset(assets=assets) pil = asset.get_image() pil.show() exifs = pil.getexif() orientation = exifs[274] print(orientation) if orientation == 6: pil = pil.rotate(90) pil.show()
-
@cvp : Hi, thanks a lot this works fine but i am always getting black Bars around the corner so that it is a square. Just if i only use your script. I got this problem the first time when i switch to the new Test Flight version of pythonista. With the standard one everything was okay.
BR kami
-
-
-
@kami note that if you get asset.get_ui._image and display it, the rotation is automatic
If you use PIL Image and rotate, add expand parameter
pil = pil.rotate(90, expand=True)
And print pil.size before and after rotate, with or without the expand parameter to compare its effect
Edit1:I think that problem could come from this: after pil.rotate, the orientation exif is still 6, thus showing the PIL.Image or displaying in an ui.Image uses this internal orientation flag erroneously.
We can modify an exif and save the pil as a new file
exifs[274] = 1 pil.save('myfile.jpg', quality=95, exif=exifs)
But that becomes complex and perhaps there is an easier way
Edit2: For info, personally, to check this orientation flag of a photo, I use the free app ViewExif to which you can share a photo and see something like
-
@cvp Hi thanks a lot for the good help, but my problem is, that if i just use your sourcecode :
import photos assets = photos.get_assets() asset = photos.pick_asset(assets=assets) pil = asset.get_image() pil.show() exifs = pil.getexif() orientation = exifs[274] print(orientation) if orientation == 6: pil = pil.rotate(90) pil.show()
all pictures with orientation 6 are shown like this in the Console. So the problem is before rotating.
If i take a picture with orientation 1 it is fine: -
@kami I know but did you try the expand parameter of rotate and check the size of the pil before or after the rotate., and the orientation flag after rotate, the flag stays 6, that is annoying
-
@kami please try this with a photo with orientation = 6. I think it solves your problem
import photos from PIL import Image import io def ui2pil(ui_img): return Image.open(io.BytesIO(ui_img.to_png())) def main(): assets = photos.get_assets() asset = photos.pick_asset(assets=assets) uii = asset.get_ui_image() pil = ui2pil(uii) pil.show() main()
If you print pil.size, you will see that it is a vertical photo. Get_ui_image has taken in account the orientation exif to convert to a vertical photo without this flag thus no more "to be rotated"
-
@cvp Hi, thanks a lot. This works fine. All pictures are now in the right size and orientation.
BR kami
-
@kami Finally, we got it 👍