(none) imager-devel
/ help / lists / applications / search /

Re: scaling animated GIFs

From: Tony Cook (
Date: Fri 15 Jun 2007 - 00:40:31 GMT

  • Next message: Bernhard Graf: "Re: scaling animated GIFs"

    On Thu, Jun 14, 2007 at 06:29:23PM +0200, Bernhard Graf wrote:
    > Hi everyone,
    > I'm trying to find the right way to scale animated GIFs.
    > While this task is easy in Image::Magick and impossible in GD, in Imager
    > it seems possible, but cumbersome.
    > ATM my example code looks like this (w/o error checking):
    > my $img = Imager->new;
    > my @in = $img->read_multi(file => $infile, type => 'gif');
    > my @out;
    > for my $i (@in) {
    > push @out, $i->scale(xpixels => 60, ypixels => 60, type=>'min');
    > }
    > $img->write_multi({file => $outfile, type => 'gif'}, @out);
    > The resulting GIF only shows the last image of the animation, presumably
    > because the resulting Image objects issued by the scale() method don't
    > have any tags, as for example "gif_delay".
    > Is that deliberate or is there an easy way to keep the tags (or
    > transform for gif_left/ _top/ _screen_width/ _screen_height)?

    In general Imager considers the tags on an image to be opaque and
    doesn't try to manipulate them, beyond populating them when the image
    is read in and using them when the image is written.

    Unfortunately you'll need to transform/copy the tags yourself.

    Note that in your case, if the images have non-zero gif_left/gif_top
    values, presumably they're smaller than the gif_screen_width /
    gif_screen_height, so scaling them all to the same size will produce
    some strange results.

    In most cases I expect you'll just need to copy gif_delay:

     # you don't need to create an image object for read_multi(), since
     # it's a class method
     my @in = Imager->read_multi(file => $infile, type => 'gif')
            or die Imager->errstr;
     my @out;
     for my $i (@in) {
       my $out = $i->scale(xpixels => 60, ypixels => 60, type=>'min');
       $out->settag(gif_delay => $i->tags(name=>'gif_delay'));
       push @out, out;
     Imager->write_multi({file => $outfile, type => 'gif'}, @out)
            or die Imager->errstr;