Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

acme (189)

  (email not shown publicly)

Leon Brocard (aka acme) is an orange-loving Perl eurohacker with many varied contributions to the Perl community, including the GraphViz module on the CPAN. YAPC::Europe was all his fault. He is still looking for a Perl Monger group he can start which begins with the letter 'D'.

Journal of acme (189)

Friday January 03, 2003
10:22 AM

EXIF goodness

[ #9741 ]
The cool Ixus v3 I got for Christmas has a neat feature: intelligent orientation.

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";

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I do some similar stuff with rotation, but I manually figure out which ones need to be rotated. My Nikon Coolpix 995 apparently has the Orientation field (which I didn't know about), but it is always top_left. Drat! I thought this would be a timesaver for me. :-)
  • Wouldn't it be better if the content of the field was changed while running the program. Currently the second run of the program turns all images that were turned further.
    • Yup, I agree. At the time, I couldn't figure out how to get Image::Info to do this, however. Patches welcome!
      • You can try exiftran instead of jpegtran :)

        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.