Derotating JPEGs with exiftool

After importing photos from my digital camera, I often find that pictures I took in landscape mode are rotated to portrait mode. Rotating photos is no big deal—I can’t imagine any image editor that won’t let you do it—but I want the rotation to be done losslessly and I want to know it’s being done losslessly. Enter exiftool, an extremely full-featured program and Perl library for editing the EXIF metadata in image files. With exiftool, I can remove the spurious rotation that sometimes appears, and I know that it’s a lossless operation because exiftool doesn’t touch the image itself, only the metadata.

For a few years now, digital cameras have included a device that tracks the orientation of the camera and puts that information into the image file’s metadata. Applications that display images read that information and present the image rotated accordingly. The orientation information is expressed as an integer and written to a specific field in the EXIF metadata for the image.

If the camera is in landscape orientation with the bottom of the camera down, the orientation value is 1:

We can use exiftool to tell us this by executing

exiftool -Orientation -n <imagename>

and out will pop

Orientation                     : 1

If we leave out the -n option, exiftool will tell us the orientation in English:

exiftool -Orientation <imagename>

gives

Orientation                     : Horizontal (normal)

If the camera is rotated so the top is pointing to the photographer’s left, the image will look like this on our computer

and exiftool will tell us the orientation as

Orientation                     : 8

or

Orientation                     : Rotate 270 CW

which tells the software on the computer to rotate the image data 270° clockwise before displaying it.

Finally, if the camera is rotated so its top is pointed to the photographer’s right, the image will look like this on our computer

and exiftool will tell us the orientation as

Orientation                     : 6

or

Orientation                     : Rotate 90 CW

So far, so good. The photos of Dr. Doom appeared on my computer with the correct orientation because my computer looked at the Orientation field of the EXIF data and rotated the image accordingly.

Unfortunately, I happen to take a lot of photographs looking down, with the camera’s image sensor in a nearly horizontal plane. In this position, the orientation device—which presumably uses gravity to figure out which way is up—doesn’t have much to go on, and the value of the Orientation field is kind of a crapshoot. I think it reports the orientation the camera was in just before I pointed it down to take the picture. And that could be any orientation, because I never think about how I’m holding the camera before I’m ready to shoot.

When this inadvertent rotation occurs, I typically want to get rid of the rotation and put the photo in landscape orientation. Exiftool has a way of doing that. Instead of having it report the value of the Orientation field, I can have it write the value1:

exiftool -Orientation=1 -n <imagename>

will get stick the value of 1 into the Orientation field, and my computer will now display the images as if the camera had been held in the normal orientation. Doing this to the two portrait images of Dr. Doom yields

and

respectively.

I found myself doing this enough that I wrote a one-line shell script to save myself from typing “Orientation” over and over. I call it derotate:

#!/bin/bash

exiftool -Orientation=1 -n "$@"

One nice thing about derotate (which it inherits from exiftool) is that I can invoke it with as many arguments as I want:

derotate <imagename1> <imagename2> <imagename3> ...

and all of the images will be derotated.

There’s more to the Orientation field than 1, 6, and 8. A value of 3 represents an image rotated upside down (which my current camera, a Canon G10, doesn’t seem to generate), and values of 2, 4, 5, and 7 represent combinations of rotation and mirroring. You can see a full discussion here.

There’s more to exiftool, too. Read the fine manual.

Tags:


  1. By default, exiftool keeps a copy of the unaltered file in <imagename_original>, so goof-ups can be recovered from. 


2 Responses to “Derotating JPEGs with exiftool”

  1. gotjosh says:

    Thanks for sharing this simple and effective script and excellent explanation! I have questions about the Auto Rotate and Rotation Tags in the EXIF data. Your script will leave them untouched correct? Are there potential issues with having the Orientation and Rotation Tags out of sync?

    cheers, gotjosh

  2. Dr. Drang says:

    At the moment, I’m away from a library of photos I can test, but my guess is that exiftool changes only the Orientation tag. You can easily test it yourself once you have exiftool installed.

    I’ve never had problems using photos I’ve derotated.