nix, shell, perl, php, mysql and mac os x tips and tricks

Friday, July 15, 2005

File images in a horizontal or vertical subdirectory - uses ImageMagick

#!/usr/bin/perl

# check the image size and move to a horizontal
# or vertical subdirectory
use Image::Size;
use File::Copy;
use strict;

my $dir = "/home/rory/Jobs/images/cig_images/";
my $horiz = $dir . "horizontal";
my $vert = $dir . "vertical";

# read the files
opendir(DIR, $dir) || die "can't opendir $dir: $!";
my @images = readdir(DIR);
closedir(DIR);

# loop thru images
foreach my $image (@images) {
    my $fullpath = $dir . $image;
    if (not(-d $fullpath)) {    # exclude directories
       
        # check what dimension of the image is longer
        my ($x, $y) = imgsize("$fullpath");
        if ($x>$y) {
            move("$fullpath", "$horiz/$image") || die "move failed: $!";
        } else {
            move("$fullpath", "$vert/$image") || die "move failed: $!";   
        }
        print "$fullpath $x $y\n";
       
    } # end if for not a directory
   
} # end foreach loop thru images