use Image::ExifTool 'ImageInfo'; $ver = "1.0"; # converts AutoRotate codes to the needed number of degrees %rotTable = ( 1 => 90, 2 => 180, 3 => 270 ); $quality = 92; # jpg quality similar to HQ setting on camera $tempFN = "TEMP"; # temp filename $|=1; # stdout buffering off $mask = $ARGV[0]; # mask if (!$mask or $mask =~ /^-/) { $me=$0; $me =~ s/.*[\/\\]//; print < Rotates images depending on their AutoRotate EXIF tag. Tested with Canon S410 images. Rotated images get their AutoRotate tag set to zero (so subsequent runs won't re-rotate). ImageMagick must be in the path, and the the Image::ExifTool library for Perl is needed. EOL exit; } print "Globbing '$mask'...\n"; my @files = glob($mask); print "Auto-rotating ".(scalar@files)." file".((scalar@files)==1?'':'s').":\n"; for $file (@files) { print " Rotate '$file'? "; # Get hash of meta information tag names/values from an image %exif = %{ ImageInfo($file, {PrintConv=>0}) }; $rot = $rotTable{$exif{AutoRotate}}; if ($rot) { print "Yes, $rot degrees\n"; $cmd = "convert -rotate $rot -quality $quality $file $file\n"; print " % $cmd\n"; system $cmd; print " Editing EXIF tags...\n\n"; $exifTool = new Image::ExifTool; # Create a new Image::ExifTool object $exifTool->SetNewValue('AutoRotate',"None"); # Set a new value for tag if (-e $tempFN) { unlink $tempFN } # Write new meta information to a file $success = $exifTool->WriteInfo($file, $tempFN); if (-e $tempFN) { # write ok, make the temp file into the real one unlink $file; rename $tempFN, $file; } else { die "Error writing file with modified EXIF tags, aborting.\n"; } } else { print "No\n"; } }