Skip to content

Instantly share code, notes, and snippets.

@jzawodn
Created November 21, 2012 22:14
Show Gist options
  • Save jzawodn/4128212 to your computer and use it in GitHub Desktop.
Save jzawodn/4128212 to your computer and use it in GitHub Desktop.
Picture Orientation
#!/usr/bin/perl -w
#
# rotate a bunch of images to upright
use strict;
use File::Copy;
use Image::EXIF;
use Data::Dumper;
$|++;
my $filename;
my $basename;
my $exif = new Image::EXIF;
my $dry_run = 1;
mkdir 'new' or die "mkdir 'new': $!";
my @filelist;
## Grab file listing...
if (not @ARGV) {
opendir D, '.' or die "$!";
@filelist = grep { /\.jpg$/i } readdir D;
closedir D;
}
else {
@filelist = @ARGV;
}
@filelist = sort @filelist;
my $num_files = @filelist;
my $count = 0;
foreach $filename (@filelist) {
$filename =~ m/(.*)\..*/o;
$basename = $1;
$count++;
print "[$count/$num_files] $basename: ";
$exif->file_name($filename);
my $info = $exif->get_image_info();
my $orient = $info->{'Image Orientation'};
#print Data::Dumper->Dump([$info]);
if ($orient eq 'Top, Left-Hand') {
## no rotate
print "000 deg";
copy $filename, "new/$filename" or die "$!";
}
elsif ($orient eq 'Left-Hand, Bottom') {
## rotate left 270
print "270 deg";
`convert -rotate 270 $filename jpeg:new/$filename`;
}
elsif ($orient eq 'Right-Hand, Top') {
## rotate left 90
print "90 deg";
`convert -rotate 90 $filename jpeg:new/$filename`;
}
elsif ($orient eq 'Unknown') {
## rotate left 270
print "none deg";
`convert -rotate 270 $filename jpeg:new/$filename`;
}
else {
## keep as is
print "??? deg";
copy $filename, "new/$filename" or die "$!";
}
print "\n";
}
exit;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment