Thursday, May 22, 2008

Perl - read a bunch of files

# read in a directory trying to ignore non-image files

#!/usr/bin/perl

use Image::Magick;

my $image = Image::Magick->new;
$image->Set(bordercolor=>'#FFFFFF');

# args to the script:
my $argc = $#ARGV+1;

my @imageFilenames, my $target;
my @imageInfo;
my $inPath = $ARGV[0] . '/';
my $outPath = $ARGV[1] . '/';

opendir DIR, $inPath or die "cannot access $path\n";
@imageFilenames = grep{$_ ne '.' && $_ ne '..'} readdir DIR;
closedir DIR;

# process all the filenames
my $j = 0;
foreach $imageFilename (@imageFilenames) {
$target = join("", ($inPath, $imageFilename));
# test if the file returns width in pixels at least 1 - meaning that
# imagemagick can truly read it
@imageInfo=$image->ping($target);
if($imageInfo[1]<1) {
next; # ie. skip this iteration
}
status =" $image-">Read($target);
warn "error reading: $status\n" if $status;
print "reading $target\n";
}

undef $image;
exit;

No comments: