#!/usr/bin/perl for my $file (@ARGV) { my $portrait = 0; # Portrait orientation? # Get dimensions of image file my $width = `identify -format %w $file 2>/dev/null` * 1.0; my $height = `identify -format %h $file 2>/dev/null` * 1.0; # Skip non-image files next if ($width <= 0) or ($height <= 0); if ($width < $height) { # Portrait orientation, change dimensions $portrait = 1; ($width, $height) = ($height, $width); } my $ratio = $width/$height; # X/Y ratio of current image my $newratio = 3.0/2.0; # Desired X/Y ratio my $r = $ratio/$newratio; next if ($r < 1.01) and ($r > 0.99); # Skip image with desired X/Y ratio # Calculate new dimensions, store as integer my $newwidth = $width; my $newheight = sprintf("%d", $height*$r); my $xoffset = 0; my $yoffset = sprintf("%d", $height*(1-$r)/2.0); if ($portrait) { # Portrait orientation, change back the dimensions ($width, $height) = ($height, $width); $newwidth = $newheight; $newheight = $height; $xoffset = $yoffset; $yoffset = 0; } # Do a loss-less JPEG cropping to the new dimensions system("jhead -cmd \"jpegtran -crop ${newwidth}x${newheight}+$xoffset+$yoffset &i > &o\" $file"); }