Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I am trying to create the simplest possible animated gif using GD, but I am failing. What am I doing wrong? Needless to say, making a static gif image works just fine. The animated gif just comes out black.

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; }
--

when small people start casting long shadows, it is time to go to bed

In reply to Creating an animated gif with GD by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2014-04-18 01:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (460 votes), past polls