Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
script to increase an images size to a 4x6 ratio in order to print without cropping
use File::Find;
my $baseDir = '/Volumes/Data/pictures/';
my $cmdIdentify = '/usr/local/bin/identify';
my $cmdConvert = '/usr/local/bin/convert';
find(\&process, $baseDir);
sub process {
my $file = $File::Find::name;
next if ($file !~ /\.jpg$/);
# get the dimension of the file
my $idOut = `$cmdIdentify -verbose "$file" | grep "Geometry"`;
chomp($idOut);
$idOut =~ s/.* //g;
print "IDOUT:$idOut;\n";
my ($w,$h,$x,$y) = split(/[+x]/,$idOut);
print "WIDTH:$w;\nHEIGHT:$h;\n";
my $newH = 0;
my $newW = 0;
#determine ratio
my $ratio = 0;
my $land = 0;
if ($h > $w) {
$ratio = ($w/$h);
} else {
$ratio = ($h/$w);
$land = 1;
}
print "RATIO IN:$ratio;\n";
print "LAND: $land;\n";
#determine new size.
if ($ratio > 0.68) {
print "RATIO > .67\n";
if ($land) {
print "LANDSCAPE\n";
$newW = ($h/0.67);
$newH = $h;
} else {
$newH = ($w/0.67);
$newW = $w;
}
} elsif ($ratio < 0.64) {
print "RATIO < .67\n";
if (! $land) {
$newW = ($h*0.67);
$newH = $h;
} else {
print "LANDSCAPE\n";
$newW = $w;
$newH = ($w*0.67);
}
} else {
$newH = $h;
$newW = $w;
}
print "NEWH:$newH;\n";
print "NEWW:$newW;\n";
print "RATI:".($newH/$newW)."OR".($newW/$newH).";\n";
# create the new image
my $outFile = $file;
$outFile =~ s/\.jpg/_FIXED.jpg/g;
`$cmdConvert "$file" -gravity center -background white -extent ${newW}x${newH} "$outFile"`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment