Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable in foreach loop not updated

Status
Not open for further replies.

llafriel

Technical User
Oct 27, 2008
1
NO
Hi,

I'm having trouble getting a little Perl program to work. It doesn't want to update some variables inside a foreach loop. '$image->Read($listitem);' works in the first iteration, but isn't updated next time around.

Pardon if this the solution is obvious, this is my first attempt at perl.

#!/usr/local/bin/perl
use Image::Magick;

my $progdir = 'c:/CoverArt/';
my $origdir = 'c:/CoverArt/Originals/';
my $pdadir = 'c:/CoverArt/PDA/';

chdir( $progdir ) or die "Cant chdir to $dir $!";

unlink("nytt.jpg");


chdir;

my($image, $x);
$gradient = Image::Magick->new;
$bottombar = Image::Magick->new;
$shine = Image::Magick->new;
$shine2 = Image::Magick->new;
$main = Image::Magick->new;
$main2 = Image::Magick->new;
$image = Image::Magick->new;

$main->Read('main.jpg');
$shine->Read('shine.png');
$bottombar->Read('bottombar.png');
$gradient->Read('gradient.png');


chdir( $origdir ) or die "Cant chdir to $dir $!";

opendir MYDIR, ".";
@contents = grep !/^\.\.?$/, readdir MYDIR;
closedir MYDIR;


foreach my $listitem ( @contents )
{
print $listitem . "\n";


$shine2 = $shine;
$main2 = $main;

chdir( $origdir ) or die "Cant chdir to $dir $!";

$image->Read($listitem);

$width = $image->Get('columns');
$height = $image->Get('rows');
$width = (305/$height)*$width;
$image->AdaptiveResize(width=>$width,height=>305);
$shine2->AdaptiveResize(width=>$width);
$image->Composite(image=>$shine,compose=>'over');
$image->Border(bordercolor=>'white',width=>'1', height=>'1');
$reflection = $image->clone();
$reflection->Flip();
$main2->Composite(image=>$image,compose=>'over', y=>-20, gravity=>'center');
$main2->Composite(image=>$reflection,compose=>'blend', y=>285, gravity=>'center');
$main2->Composite(image=>$gradient,compose=>'over', gravity=>'south');
$main2->Composite(image=>$bottombar,compose=>'over', gravity=>'south');


chdir( $pdadir ) or die "Cant chdir to $dir $!";
$main2->Write($listitem);

}
 
You create $image as a new Image::Magick before the loop, and then on each loop iteration you call $image->Read.

According to the examples on the perlmagick page, I would assume that multiple Read calls actually adds the new image to a kind of stack of already loaded images (for example, to read multiple images and output them into an animated GIF image).

i.e. like this example from perlmagick:

Code:
  #!/usr/local/bin/perl
  use Image::Magick;

  my($image, $p, $q);

  $image = new Image::Magick;
  $image->Read('x1.png');
  $image->Read('j*.jpg');
  $image->Read('k.miff[1, 5, 3]');
  $image->Contrast();
  for ($x = 0; $image->[$x]; $x++)
  {
    $image->[$x]->Frame('100x200') if $image->[$x]->Get('magick') eq 'GIF';
    undef $image->[$x] if $image->[$x]->Get('columns') < 100;
  }
  $p = $image->[1];
  $p->Draw(stroke=>'red', primitive=>'rectangle', points=>20,20 100,100');
  $q = $p->Montage();
  undef $image;
  $q->Write('x.miff');

So, try creating $image = new Image::Magick inside your foreach loop, so that a *new* Image::Magick is created for each image you're looping through and then destroyed when the loop goes out of scope, instead of re-using the same object.

(to that end, your object probably *is* being updated, but you're probably writing to a format that doesn't support animation so it only writes the first image on the stack)

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top