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

Hello all:

I am trying to consolidate some ImageMagick tasks with some arrayed data.

I have this:

my $imagen0 = Image::Magick->new; $imagen0->Set(size=>'128x70'); $imagen0->ReadImage('canvas:transparent'); $imagen0->Annotate(font=>"/fonts/trebucbd.ttf", pointsize=>24, fill=> +$fontcolor1, gravity=>'Center', text=>$p12hrn0); my $imagen1 = Image::Magick->new; $imagen1->Set(size=>'128x70'); $imagen1->ReadImage('canvas:transparent'); $imagen1->Annotate(font=>"/fonts/trebucbd.ttf", pointsize=>24, fill=> +$fontcolor1, gravity=>'Center', text=>$p12hrn1); my $imagen2 = Image::Magick->new; $imagen2->Set(size=>'128x70'); $imagen2->ReadImage('canvas:transparent'); $imagen2->Annotate(font=>"/fonts/trebucbd.ttf", pointsize=>24, fill=> +$fontcolor1, gravity=>'Center', text=>$p12hrn2); ...10 total
and then this:
$image->Composite(geometry => '+188+675', image => $imagen0 ); $image->Composite(geometry => '+365+675', image => $imagen1 ); $image->Composite(geometry => '+542+675', image => $imagen2 ); ...10 total

And I am trying to simplify in a loop with this:

for my $i (@$times) { my $imagen($i) = Image::Magick->new; $imagen($i)->Set(size=>'128x70'); $imagen($i)->ReadImage('canvas:transparent'); $imagen($i)->Annotate(font=>"/fonts/trebucbd.ttf", pointsize=>24, +fill=> $fontcolor1, gravity=>'Center', text=>$i); } my $dayy = 675; my $dayx = 188; my $dayoff = 177; my $daygeo = ""; for my $i (@$times) { $daygeo = "+" . $dayx . "+" . $dayy; $image->Composite(geometry => $daygeo, image => $i ); $dayx = $dayx + $dayoff; }

The array $times is previously populated with text (Wednesday Night, Thursday, Thursday Night, ect.). I am guessing that it has something to do with creating an array for $imagen with ImageMagick, but I'm not quite sure how else to do this right now.

Thanks for your wisdom as always!

John

Replies are listed 'Best First'.
Re: Loop array with ImageMagick
by zentara (Cardinal) on Jul 19, 2012 at 10:53 UTC
    The array $times is previously populated with text (Wednesday Night, Thursday, Thursday Night, ect.). I am guessing that it has something to do with creating an array for $imagen with ImageMagick, but I'm not quite sure how else to do this right now.

    Hi, it's a little known fact-of-life that IM maintains a global stack of images that it uses for its own purposes, so you need to clear out the IM object after every run thru the loop. Whenever you read an image, it gets pushed into an internal array for storage. This useful sometimes, as in certain IM methods, all images can be processed by going thru this array. Of course, it also can get in the way sometimes, as you have seen. What you you probably need to do is undef @$my_IM_object, as shown here:

    my $image = Image::Magick->new; foreach my $pic (@pics){ my $ok; $ok = $image->Read($pic) and warn ($ok); $image->Scale(geometry => '100x100'); $ok = $image->Write($thumb) and warn ($ok); # this is the key line undef @$image; #needed if $image is created outside loop
    You can also read perldoc -q clear and read the first section "How do I clear out a package?".

    Of course, this is just my top-of-the-morning guess at your problem, because your code dosn't even look like it will run correctly with things like my $imagen($i) =. That dosn't match any data structure commonly used in Perl.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Loop array with ImageMagick
by Anonymous Monk on Jul 19, 2012 at 08:00 UTC
Re: Loop array with ImageMagick
by aitap (Curate) on Jul 19, 2012 at 09:27 UTC

    What error message do you receive? Can you show your array using Data::Dumper or x command on the Perl debugger?

    Sorry if my advice was wrong.
Re: Loop array with ImageMagick
by johnfl68 (Scribe) on Jul 19, 2012 at 19:06 UTC

    Well, I was just trying to shorten up a repetitive task.

    The variables and arrays names are based on the way the data is formed in the xml that is being read.

    And all the images are part of 1 master composition, so I don't want them cleared out, because they still need to be used.

    Thanks for the help, but it looks like there isn't a way to shorten this up, so I will just stick with doing it the long way I guess.

    John