Using EXIF information to rotate an image

EXIF stands for Exchangeable Image File Format. It is basically an de-facto specification that almost every single digital camara in the world is using today.

This specification defines how extra data could be embedded in a JPEG or TIFF image, so when you make a photo with your digital camera, saves  extra information about which camera the photo was made, the image size, resolution, exposure time, aperture, … and the important thing is, it saves the orientation.

For example, if you make a vertical photo, then is not the same orientation that when you do a landscape photo.

The problem with the digital photos, is that if you get that vertical photo, and you upload it to a server, and you take a look on the browser, the image will be rotated, it is not shown as you might expect. Lucky us, we can use EXIF information to automatically detect the orientation of the photo and use that information to auto orient the image.

Almost all image manipulation programs do this automatically. Even if you are visualizing photos, you won’t probably notice this problem because almost all of them do this automatically. But as this is a programming blog, the interesting part is how to automate this procedure in order to use it on your web page / server, wherever.

There are two easy commands to auto-orient an image.


I have not found yet a single thing you could not do using ImageMagick, it has an amazing set of utilities.

The good news is that ImageMagick solves the problem by using -auto-orient, which is in fact very easy

The bad news is that using ImageMagick has two serious problems:

  1. For auto-orienting the image using -auto-orient ImageMagick has to read the image (JPEG) , do the transformations, and then rewrite the image into disk. This means that as JPEG is lossy, the image quality is affected on the rewrite process, and the image is “damaged”. You are not probably going to notice, but is good to know it degrades the image.
  2. The second BIG problem is that version 6.3.7 (the one in Ubuntu 8.10) DOES NOT WORK because of bug.

Example to auto orient an image:

$ convert image1.jpg -auto-orient imagen-auto-oriented.jpg

The bug seems to be solved on the latest versions (6.4.x), but anyway, the first problem could not be avoided using ImageMagick utilities, so here it is the second commnand:


JHead is a program for manipulating EXIF information in JPEG images, transfer information between images, …

The interesting thing is -autorot, that basically do what we pretend ,rotate automatically the images using EXIF information BUT without degrading the original image.

Following there is a simple example for rotating automatically all images in current folder:

$ jhead -autorot *.jpg

Trackback URL

, , , , , , ,

Comments are closed.