I found using Imager and Imager::File::JPEG the easiest solution. Works on Ubuntu, and I assume other Linux flavors, not on Windows
here's a simple script that iterates the .jpg files in a directory and resizes them.
use strict;
use warnings;
use Imager;
use Imager::File::JPEG;
my $folder = shift;
my $dir = "/media/mark/My Passport/archive"; #starting point
$dir = $dir . '/'. $folder; #target subdirectory
chdir($dir);
my $dh;
opendir($dh, $dir);
while(readdir($dh)){
my $fn = $_;
next unless $fn=~ m/\.jpg$/;
my $newimg;
print "checking $fn\n";
my $img=Imager->new(file=>$fn,type=>'jpeg');
my ($height, $width);
$height = $img->getheight;
$width = $img->getwidth;
my $orient = 'Portrait'; #default
if ($height < $width){
$orient = 'Landscape';
}
print "processing $fn height->$height width=$width orient = $orien
+t\n";
if($orient eq 'Portrait' && $height < 800){
$newimg = $img->scale(ypixels=>900);
}
elsif($orient eq 'Landscape' && $width < 1200){
next if ($height / $width) >= 0.75;
$newimg = $img->scale(xpixels=>1200);
}
else{ #dimensions OK, nothing to do
next;
}
$height = $newimg->getheight;
$width = $newimg->getwidth;
print "saving $fn orientation->$orient, width->$width height->$hei
+ght\n";
$newimg->write(file=>$fn, type=>'jpeg',jpegquality=>100)
or die "cannot save altered image $img->errstr";
+
}
|