use strict; use warnings; use feature 'say'; use Imager; use Image::PNG::Rewriter; use IO::Compress::Deflate 'deflate', ':constants'; use Compress::Zopfli::ZLIB 'compress'; my $name = 'PNG_transparency_demonstration_1.png'; open my $fh, '<', $name; binmode $fh; Imager-> new( fh => $fh ) -> write( type => 'png', data => \my $png_imager ); seek $fh, 0, 0; my $rw = Image::PNG::Rewriter-> new( handle => $fh, zlib => sub { my $input = shift; deflate \$input => \my $output, -Level => 9, -Strategy => Z_FILTERED; return $output } ); $rw-> refilter( $rw-> original_filters ); # no-op my $png_zlib9 = $rw-> as_png; seek $fh, 0, 0; $rw = Image::PNG::Rewriter-> new( handle => $fh, zlib => \&compress, ); $rw-> refilter( $rw-> original_filters ); # no-op my $png_zopfli = $rw-> as_png; my $f = "%-24s%s\n"; printf $f, 'Original file', -s $name; printf $f, 'Re-saved with Imager', length $png_imager; printf $f, 'Compressed with zlib', length $png_zlib9; printf $f, 'Compressed with zopfli', length $png_zopfli; __END__ Original file 179559 Re-saved with Imager 203632 Compressed with zlib 198642 Compressed with zopfli 179618