When I learnt about this I thought it'd be wonderful - when I put photos online, having to rotate images is a pain. However, the camera doesn't actually rotate the image, all it does it set a flag in the EXIF header. Perl comes to the rescue, as does jpegtran. It'd be silly if I rotated photos by decompressing the JPEG, rotating the image, and recompressing it. JPEG is lossy, so I'd lose quality. jpegtran exploits features of the JPEG spec allowing lossless rotation. Here's the script I knocked up:
#!/usr/bin/perl -w
use strict;
use File::Copy;
use File::Temp qw(:POSIX);
use Image::Info qw(image_info);
$| = 1;
foreach my $filename (@ARGV) {
print "$filename... ";
my $info = image_info($filename);
my $orientation = $info->{Orientation}->[1];
if ($orientation eq 'top_left') {
print "not changing\n"
} elsif ($orientation eq "right_top") {
print "rotated right\n";
my $tmp = tmpnam();
system("jpegtran -copy all -rotate 90 $filename > $tmp") == 0
or die "$?";
move($tmp, $filename);
} elsif ($orientation eq "left_bot") {
print "rotated left\n";
my $tmp = tmpnam();
system("jpegtran -copy all -rotate 270 $filename > $tmp") == 0
or die "$?";
move($tmp, $filename);
} else {
print "unknown, not changed\n";
}
}
Sweet (Score:2)
Writing back the changes of the orientation (Score:1)
Re:Writing back the changes of the orientation (Score:2)
Re:Writing back the changes of the orientation (Score:1)
Debian's description
exiftran is a command line utility to transform digital image jpeg
images. It can do lossless rotations like jpegtran, but unlike
jpegtran it cares about the EXIF data: It can rotate images
automatically by checking the exif orientation tag, it updates the
exif informaton if needed (image dimension, orientation), it also
rotates the exif thumbnail. It can process multiple images at once.