Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Imager::GIF seems broken

by Anonymous Monk
on Jan 05, 2020 at 14:27 UTC ( [id://11110979]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to use Imager::GIF "a handy module for animated GIF processing" to scale images but get an error when trying to write:

Usage: i_writegif_wiol(IO,hashref, images...) at .../Imager/File/GIF.pm line 84.

The module has no real tests (just one: will it load) so it looks good to the Testers. I can see what it's trying to do and could cargo-cult it into my own code but I can't see what's wrong, can you?

https://metacpan.org/release/Imager-GIF/source/lib/Imager/GIF.pm https://metacpan.org/release/Imager-File-GIF/source/GIF.pm#L84

#!/usr/bin/perl use strict; use warnings; use Imager::GIF; my $gif = shift || die 'need an image'; my $img = Imager::GIF->new; $img->read(file => $gif) or die $img->errstr; $img = $img->scale(scalefactor => 0.5); $img->write(file => join '.', $gif, 'half', 'gif') or die $img->errstr;
Thanks for your attention to this matter.

Replies are listed 'Best First'.
Re: Imager::GIF seems broken
by vr (Curate) on Jan 05, 2020 at 15:15 UTC

    Hi, apart from, contrary to Synopsis even, the write method expecting file name (not options hash), everything else seems to work per POD: the read is to be used as constructor (class method):

    use strict; use warnings; use Imager::GIF; my $gif = shift || die 'need an image'; my $img; $img = Imager::GIF-> read( file => $gif ) or die $img-> errstr; $img = $img-> scale( scalefactor => 0.5 ); $img-> write( join '.', $gif, 'half', 'gif' ) or die $img-> errstr;
      Thank you vr! The module works as expected when following your instructions. Apparently the documentation synopsis contains two severe errors.

      Imager::GIF synopsis:

      my $sonic = Imager::GIF->new(file => 'sonic.gif');
      
      Should be:
      my $sonic = Imager::GIF->read(file => 'sonic.gif');
      
      Imager::GIF synopsis:
      $small_sonic->write(file => 'small_sonic.gif');
      
      Should be:
      $small_sonic->write('small_sonic.gif');
      
      The first problem, not using the read method to construct the object, was causing the write error. The second issue, supplying a hash instead of file name to write method, silently fails to do anything.

      Kudos

Re: Imager::GIF seems broken
by marto (Cardinal) on Jan 05, 2020 at 15:04 UTC

    I can't speak for Imager::GIF, but simply replacing it with Imager in your example works without issue.

    #!/usr/bin/perl use strict; use warnings; use Imager; my $gif = shift || die 'need an image'; my $img = Imager->new; $img->read(file => $gif) or die $img->errstr; $img = $img->scale(scalefactor => 0.5); $img->write(file => join '.', $gif, 'half', 'gif') or die $img->errstr;
      Thanks marto but your example demonstrates why we need Imager::GIF. Using Imager to transform animated GIFs will only save the first frame of the animation. Imager::GIF does the dirty work of taking apart the animation, transforming each image, and putting it back together again.

        I'm glad you found a solution, your previous post would make a good patch.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11110979]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-06-13 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.