igal auto rotation of images

igal is a very nice perl script which creates html picture galleries. I use this script for my photos since it’s easy to use and I like the film reel effect in the galleries.
But there is one annoying point: It does not detect, if pictures are rotated (if they have a rotation flag in the EXIF header). So sometimes pictures appeared wrongly rotated in the gallery and you had to rotate them manually and recreate the gallery.
So I added the functionality to the igal script, which was not as hard as I thought first. It uses jhead, a program to manipulate the EXIF header of a JPEG file. If you run jhead on a rotated file, it gives you information like this:

kati@hitchhiker:/media/sda1/DCIM/132CANON$ jhead IMG_3263.JPG
File name : IMG_3263.JPG
File size : 830969 bytes
File date : 2006:04:01 20:00:26
Camera make : Canon
Camera model : Canon DIGITAL IXUS v2
Date/Time : 2006:04:01 20:00:28
Resolution : 1600 x 1200
Orientation : rotate 90
Flash used : Yes (auto)
Focal length : 5.4mm (35mm equivalent: 37mm)
CCD width : 5.23mm
Exposure time: 0.013 s (1/80)
Aperture : f/2.8
Whitebalance : Auto
Metering Mode: matrix

I can use this information to detect if the pic is rotated by searching for the orientation flag and read the value (90 in this case). The Perl code in the igal script for detecting and autorotating is this:

print "Check if rotation is necessary ";
for($i=0; $i < $nfiles; $i++)
{
print ".";
$file=$imgfiles[$i];
my $result = `jhead $file | grep Orientation | cut -d \" \" -f5`; # return as list, one line is one element
if($result!=0)
{
chomp($result); #cut the \n at the end
print "Rotating image $file for $result degrees ...\n";
system("convert -rotate $result $file $file" );
#exif-rotation tag deletion
system("jhead -norot $file" );
}

}
print " done!\n";

All images that shall appear in the gallery are in an array $imgfiles which I can iterate. I get the jhead info for every file (image), grep for the string „Orientation“ and cut the last substring which is the orientation value (delimiter of substrings is a blank \“ \“). This value is stored in the variable $result, chomp deletes the return character at the end. It is used with the convert script (this one comes with the imagemagick package, it’s a command line script for image manipulation), which actually performs the rotation of the image. Last it runs again the jhead script and deletes the Orientation tag since the image is not rotated anymore. That’s it. Here is the complete igal script.

If you like to add the information from the EXIF header in the slides of the gallery, check out this posting on how to hack igal for doing so.

Kommentar verfassen