Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating an image with orientation specified in EXIF using Python without PIL including the thumbnail

I have the following scenario:

  • I am sending an image from iPhone along with the EXIF information to my Pyhon socket server.
  • I need the image to be properly oriented based on the actual orientation when the image was taken. I know IOS always saves the image as Landscape Left and adds the actual orientation as EXIF field (EXIF.Image.Orientation).
  • I am reading the EXIF field to see the actual orientation. Then I am rotating the image using wxpython to the proper orientation.

I am using pyexiv2 for EXIF manipulation.

Issue: The EXIF information incluiding the thumbnails lost while rotating the image using wxpython.

What I did: I am reading the EXIF before rotating the image. I reset the orientation field in the EXIF. Then I am putting it back after rotation.

The problem:

The thumbnail inside the EXIF is not rotated. So, the image and thumbnail have different orientations.

Questions?

Is there any module other than PIL to rotate an image keeping its EXIF info?

Is there a separate EXIF field for thumbnail orientation?

Is there a way I can just rotate the Thumbnail alone?

Thanks for your help...

like image 267
ATOzTOA Avatar asked Dec 14 '12 03:12

ATOzTOA


People also ask

How do you rotate an image in Python?

The imutils. rotate() function is used to rotate an image by an angle in Python.

How do I view EXIF orientation?

To view the image Exif info, open an image and click Image -> Information . If the image contains Exif info, you can then click the EXIF info button at the bottom left of the popup window to check the image Exif info.

How do I get rid of EXIF orientation?

To fix the EXIF orientation, open the image in an image editing program. Rotate the image to the correct orientation, then save the file and reupload your image. As an alternative, you can simply remove all EXIF data from images in Windows and macOS.


2 Answers

This solution works for me: PIL thumbnail is rotating my image?

Don't need to check if it's iPhone or iPad: if photo has orientation tag – rotate it.

from PIL import Image, ExifTags  try:     image=Image.open(filepath)      for orientation in ExifTags.TAGS.keys():         if ExifTags.TAGS[orientation]=='Orientation':             break          exif = image._getexif()      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.save(filepath)     image.close() except (AttributeError, KeyError, IndexError):     # cases: image don't have getexif     pass 

Before:

Before

After: After

like image 107
scabbiaza Avatar answered Sep 22 '22 09:09

scabbiaza


If you're using Pillow >= 6.0.0, you can use the built-in ImageOps.exif_transpose function do correctly rotate an image according to its exif tag:

from PIL import ImageOps  image = ImageOps.exif_transpose(image) 
like image 36
GaretJax Avatar answered Sep 22 '22 09:09

GaretJax