punkish has asked for the wisdom of the Perl Monks concerning the following question:
Update: I have successfully made an animated gif with Imager, but am still curious as to why GD doesn't work for me. On an additional note, there are several modules for working with images -- GD, Imager, PerlMagick, probably many more. Is there some comprehensive-ish comparison of the offerings out there?
use GD; my $img_width = 400; my $img_height = 300; my $cell_width = 100; my $cell_height = 100; my $num_of_years = 3; # Create a new, empty image my $img = GD::Image->new($img_width, $img_height); # Allocate some colors my $colors = colors($img); # Mark start of a new animation my $gifdata = $img->gifanimbegin; for (1 .. $num_of_years) { # Make a frame of right size my $frame = GD::Image->new($img->getBounds); image($frame, $img_width, $img_height, $cell_width, $cell_height, +$colors); # Add frame $gifdata .= $frame->gifanimadd; } # Finish the animated GIF $gifdata .= $img->gifanimend; open F, ">image.gif" or die $!; print F $img->gif(); close F; sub colors { my ($img) = @_; my @colors = (); for my $r (0 .. 7) { for my $g (0 .. 7) { for my $b (0 .. 7) { push @colors, $img->colorAllocate($r*32, $g*32, $b*32) +; } } } return \@colors; } sub image { my ($img, $img_width, $img_height, $cell_width, $cell_height, $col +ors) = @_; my $num_of_cells_in_x = ($img_width / $cell_width) - 1; my $num_of_cells_in_y = ($img_height / $cell_height) - 1; for my $y1 (0 .. $num_of_cells_in_y ) { my $ymin = $y1 * $cell_height; my $ymax = ($y1 * $cell_height) + $cell_height; for my $x1 (0 .. $num_of_cells_in_x ) { my $xmin = $x1 * $cell_width; my $xmax = ($x1 * $cell_width) + $cell_width; # Draw a filled rect of specified color my $color = $colors->[ int(rand( @$colors )) ]; $img->filledRectangle($xmin, $ymin, $xmax, $ymax, $color); } } return $img; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Creating an animated gif with GD
by BrowserUk (Patriarch) on Aug 22, 2010 at 02:39 UTC | |
by punkish (Priest) on Aug 22, 2010 at 12:41 UTC | |
by tonyc (Friar) on Aug 31, 2010 at 00:24 UTC | |
Re: Creating an animated gif with GD
by zentara (Cardinal) on Aug 22, 2010 at 11:51 UTC | |
Re: Creating an animated gif with GD
by Khen1950fx (Canon) on Aug 22, 2010 at 07:23 UTC |