Emending an esoteric Exif error

From November 2006 until June 2007 (coinciding with my use of Ubuntu’s Edgy Eft v6.10), there was a bug in the old photo importer (or in Gimp, I’m not sure which) that failed to set the Exif orientation tag to 1 (aka top-left) after rotating a photo. This was solved in the following release of Ubuntu, though the fix was somewhat incomplete—as I wrote about in How to fix Eye of Gnome’s photo orientation in Ubuntu Feisty.

What this means is that the vertical photos I edited in Gimp during that time had an Exif orientation tag indicating that the top-left of the image was something other than the top-left as it appeared when I hit save. The funny thing is that Firefox (to this day) completely ignores this orientation tag. So I had no idea there was a bug lurking in the Exif metadata of the photos on my blog.

The first time I discovered the problem came much later, on an iPhone no less. See, Safari does pay attention to the Exif orientation tag (or at least it did—I was unable to reproduce the problem on Stephanie’s iPhone 4), so when it sees a photo that says it needs to be rotated, it rotates it. Which meant Safari was rotating my already rotated photos, rendering those vertical shots from 2006–2007 horizontally in the affected blog posts. I have to say, I’m surprised no one ever brought this to my attention.

Screenshot of justinsomnia.org on an iPhone demonstrating an incorrectly rotated photo
iPhone demonstrating my erroneous Exif orientation tag (here’s how it should look: I think this is an Indian Warrior)

I hadn’t given much thought to fixing the problem, until recently. The other day I wrote a little photo gallery generator, which meant that my code had to read the Exif orientation tag in order to correctly rotate the photos before resizing them. And then last night, I happened to be checking out my photos that people have “pinned” on Pinterest, and sure enough, one had been rotated incorrectly. Much like my photo gallery generator, when Pinterest grabs a copy of a photo, they rotate it based on the Exif orientation before resizing it. Of course they had no idea that the Exif orientation was wrong and that the photo had already been rotated.

Screenshot of Pinterest demonstrating an incorrectly rotated photo
Pinterest demonstrating my erroneous Exif orientation tag

So I put 2 and 2 together and decided to write a little program in Python to figure out which of the photos on my blog had the incorrect Exif orientation tag (over 80!), and then fix them in one fell swoop. So satisfying.

#!/usr/bin/python

import glob, os, shutil
import pyexiv2 # thanks to apt-get!

path = '/home/jwatt/public_html/justinsomnia/images'

for filename in sorted(glob.glob(path + '/*.jpg')):
    
    metadata = pyexiv2.ImageMetadata(filename)
    metadata.read()
    
    try:
        tag = metadata['Exif.Image.Orientation']
        orientation = tag.value
    except:
        orientation = 1

    if orientation in [3,6,8]:
        print filename
        new_filename = os.path.dirname(filename) + \
            '/fixed/' + \
            os.path.basename(filename)
        shutil.copy2(filename, new_filename)
        
        metadata = pyexiv2.ImageMetadata(new_filename)
        metadata.read()
        tag = metadata['Exif.Image.Orientation']
        tag.value = 1
        metadata.write(preserve_timestamps=True)

1 Comment

Ahhhaha. The transformation is complete!

Care to Comment?

Or if you'd prefer to get in touch privately, please send me an email.

Name

Email (optional)

Blog (optional)