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, $colors) = @_; 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; }