Change or Remove Exif Orientation Tag in Python

I need some of my images to display in the same orientation, whether the software reads exif data or not. One solution (the only thing that could actually fit) was to rotate the image according to the exif tag, if it exists, and then remove or change the tag to "1".

Example
Say the image has an exif Orientation tag set to 3. What I want to do is rotate this image to match that tag and save it that way. So software that does not interpret exif will still display it in a good orientation. Although if the orientation of the exif tag is still 3, then the software that Exif interprets will rotate my already rotated image. So I want to set this tag to 1 (which means: lack of orientation) or delete it.

My ultimate goal is that the image will always be displayed the same, no matter what software I use to open it.

There are many questions about this, Exif and Python, blah blah blah. Here is a list of the libraries I've heard of:

  • Pyexiv2: Not suitable, I'm currently using Python 3.3 with a pad.
  • Gexiv2: It looks like a specific platform for a specific platform.
  • EXIF.py
  • Pexif : looks like the last?

What are the best practices? Is there a clean python solution? (Which I could install with pip and put it in my requirements .txt file) Is there any new lib I could use that is specific to Python3?

My only problem right now is to modify and write this exif data to an image file. I have no problem reading exif data and rotating the image according to the orientation tag. Any tips or tricks on this?

+7
python python-imaging-library exif
source share
3 answers

Note:

This answer works in Python2.7 - you can probably just swap Pillow for PIL with Python3, but I can't talk about it from experience. Please note: unlike pyexiv2 and most other packages that allow you to modify EXIF ​​metadata, the pexif library is standalone and pure python, so it does not need to link to any C libraries that may not be available on your computer.

Overview:

For two steps you need to use two separate tools:

  • pexif to change metadata (EXIF tag)
  • PIL for image rotation

part of pexif:

Four steps:

  • open image
  • check orientation (needed later for rotation)
  • change orientation to 1
  • save image

If the orientation tag is not found, the result of the AttributeError will be received, therefore try: Also note that pexif requires that the orientation tag be a list (with a single element).

 import pexif img = pexif.JpegFile.fromFile(temp_dir + filename) try: #Get the orientation if it exists orientation = img.exif.primary.Orientation[0] img.exif.primary.Orientation = [1] img.writeFile(temp_dir + filename) 

PIL Part:

Now rotate the image. The source of the meaning of the possible orientations (used to create a lookup table for rotation) is here .

  • open image
  • apply the necessary rotation / reflection
  • save image
 from PIL import Image img = Image.open(temp_dir + filename) if orientation is 6: img = img.rotate(-90) elif orientation is 8: img = img.rotate(90) elif orientation is 3: img = img.rotate(180) elif orientation is 2: img = img.transpose(Image.FLIP_LEFT_RIGHT) elif orientation is 5: img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT) elif orientation is 7: img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT) elif orientation is 4: img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT) #save the result img.save(temp_dir + filename) 

together:

 if ctype == 'image/jpeg': from PIL import Image import pexif img = pexif.JpegFile.fromFile(temp_dir + filename) try: #Get the orientation if it exists orientation = img.exif.primary.Orientation[0] img.exif.primary.Orientation = [1] img.writeFile(temp_dir + filename) #now rotate the image using the Python Image Library (PIL) img = Image.open(temp_dir + filename) if orientation is 6: img = img.rotate(-90) elif orientation is 8: img = img.rotate(90) elif orientation is 3: img = img.rotate(180) elif orientation is 2: img = img.transpose(Image.FLIP_LEFT_RIGHT) elif orientation is 5: img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT) elif orientation is 7: img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT) elif orientation is 4: img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT) #save the result img.save(temp_dir + filename) except: pass 
+11
source share

Take a look at EXIF ​​and Pyxif in this Git repository: https://github.com/hMatoba

Both offer simple and clean Python methods for reading and modifying EXIF ​​tags. In combination with PIL or Pillow, these libraries are fantastic and they do not come with the overhead of other huge packages like pyexif2.

+1
source share

You can use Exif data to store Pillow

 img.save(output, img_format, quality, exif=img_info.get('exif')) 
-2
source share

All Articles