Skip to content Skip to sidebar Skip to footer

Modify Or Delete Exif Tag 'orientation' In Python

I need some of my pictures to be displayed with the same orientation whether the software reads the exif data or not. One solution (the only one that could fit actually) would be t

Solution 1:

note:

This answer works for Python2.7 - you can probably just swap Pillow for PIL in Python3, but I can't speak to that from experience. Note that unlike pyexiv2 and most of the other packages that allow you to modify EXIF metadata, the pexif library is standalone and pure python, so it does not need to bind to any C libraries that may not be present on your machine.

overview:

You need to use two separate tools for the two steps:

  • pexif to modify the metadata (EXIF tag)
  • PIL to rotate the image

pexif part:

Four steps:

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

If the orientation tag isn't found, an AttributeError results, hence the try:. Also note that pexif needs the orientation tag to be a list (with one 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 to rotate the image. A source for the meaning of the possible orientations (used to generate the lookup table for rotations) is here.

  • open the image
  • apply the necessary rotation/reflection
  • save the image
from PIL import Image

img = Image.open(temp_dir + filename)
if orientation is6: img = img.rotate(-90)
elif orientation is8: img = img.rotate(90)
elif orientation is3: img = img.rotate(180)
elif orientation is2: img = img.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation is5: img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation is7: img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation is4: img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT)

#save the result
img.save(temp_dir + filename)

put it all 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 is6: img = img.rotate(-90)
    elif orientation is8: img = img.rotate(90)
    elif orientation is3: img = img.rotate(180)
    elif orientation is2: img = img.transpose(Image.FLIP_LEFT_RIGHT)
    elif orientation is5: img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
    elif orientation is7: img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
    elif orientation is4: img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT)

    #save the result
    img.save(temp_dir + filename)
  except: pass

Solution 2:

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

Both offer simple and pure Python methods for reading and modifying EXIF tags. In combination with PIL or Pillow, these libs are fantastic and they don't come with the overhead of other huge packages, such as pyexif2.

Solution 3:

You can use Pillow save Exif data

img.save(output, img_format, quality, exif=img_info.get('exif'))

Post a Comment for "Modify Or Delete Exif Tag 'orientation' In Python"