Friday, 1 February 2008

Exif auto-rotate & scale your photos

Typically after a journey to new place I finish with hundreds of photos from my camera. When I am browsing it on linux (or mac osx) everything is ok - gwenview can read EXIF rotation data and show them in proper direction (rotate on the fly). The problem begin when you upload such "good" portrait image or send it by email - it will be 90deg rotated. Ooops!

A small script can fix it and create subdirectories with 1280x1024 and 800x600 scaled images. It also rotates original images lossless preserving original EXIF data - don't be afraid about the quality!

To use this, simply add packages: imagemagick, exiftran & perl bindings Image::Magick.

#!/usr/bin/perl -w
`exiftran -aip *.JPG`;
`exiftran -aip *.jpg`;

use strict;
use Image::Magick;
use Cwd;

my $currdir = ( getcwd =~ m{([^/]+)$} )[0];

my $webdir = $currdir . '_to1280';
my $web800 = $currdir . '_to800';
print "Creating dirs: $webdir, $web800\n";
mkdir $webdir;
mkdir $web800;

print $| = 1;
foreach my $filename (<*.jpg>, <*.JPG>) {
# read this
my $image = Image::Magick->new();

# scale it
print "$filename ";
$image->Thumbnail(geometry => '1280x1024>');
$image->write($webdir . '/' . lc($filename));

$image->Thumbnail(geometry => '800x600>');
$image->write($web800 . '/' . lc($filename));
print ".\n";