dstefani has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I was told, just start writing code, it will suck, but you'll get better.
Let's hope.

I'm trying to read a dir of images and then testing for dimention size, if it passes I want to cp it to another dir for digimarc processing. When using the Image::Size mod I get the following error:
Use of uninitialized value in concatenation (.) or string at digi_move.pl line 19.

Here is my code so for:

#!/usr/bin/perl -w use strict; use Image::Size; opendir(DIR,"/home/dstefani/digi/image_uploads") or die "Can't open di +rectory: $!\n"; my @images = readdir(DIR) or die "Can't read selected directory: $!\n" +; closedir(DIR); my $image; foreach $image (@images) { #print "$image\n"; # this worked, no errors my ($x, $y); ($x, $y) = imgsize($image); # this worked, no errors print "$x\n"; # TEST: this breaks it # I really want to get the images larger than 85 x 85 # and move them to a different dir for digimarc processing #if ( $x > '85' && $y > '85') #{ # system("cp $image /home/dstefani/digi/med_lg_images/$image"); #} }

I feel like it's something really simple and I'm just not seeing it.

Your help is greatly appreciated.
- dstefani

Replies are listed 'Best First'.
Re: Images::Size troubles
by CombatSquirrel (Hermit) on Oct 14, 2003 at 16:55 UTC
    The only thing I see at the moment is that in ($x, $y) = imgsize($image); and only there you are using a relative path. readdir does not return an absolute path; try ($x, $y) = imgsize("/home/dstefani/digi/image_uploads/$image"); instead and see if it works.
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
      That Sir, was the ticket!

      Thank you,
      -dstefani

Re: Images::Size troubles
by DrHyde (Prior) on Oct 14, 2003 at 19:18 UTC
    Seeing that you're trying to learn, I'll make a couple of suggestions for ways your script could be improved.

    Firstly, because I often fall into the exact same trap that you did, I like to do something like this:

    use Cwd; ... my $dir = getcwd; my @images = map { "$dir/$_" } readdir(DIR);
    which turns the list of directory entries into a list of unambiguous fully-qualified names. map takes an action (in this case a little block of code) and a list on its right hand side, spitting another list out on the left which has been transformed using the specified action. See the docs for a more lucid explanation than I can give :-)

    Second, readdir(DIR) gives you a list of everything in the directory - image files, other files, subdirectories, plus the two special directory entries '.' and '..'. You can easily winnow the list down like so:

    @images = grep { -f && # only allow files, not directories /\.(png|gif|jpe?g|.....)$/i } map { ... } readdir(DIR);
    grep is sort of like map, except that instead of transforming the list, it filters the list. The list it spits out on its left-hand side will consist solely of those elements of the input list for which the action evaluates true - in this case, will consist solely of those elements which are filename as opposed to directory names, and whose filenames end in .png, .gif, .jpeg, .jpg etc. Again, see the docs for a full explanation. map and grep are very powerful tools and they save my bacon day after day.
Re: Images::Size troubles
by jeffa (Bishop) on Oct 14, 2003 at 17:36 UTC