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


Image::PNG::Rewriter - Rewrite and Refilter PNG images


  use Image::PNG::Rewriter;

  open my $h, '<', '...';
  binmode $h;
  my $re = Image::PNG::Rewriter->new(handle => $h, zlib => \&zlib);

  my $width = $re->width;
  my $height = $re->height;
  my $mode = $re->color_mode;
  my $depth = $re->depth;
  my @chunks = $re->original_chunks;
  my $orig_deflated = $re->original_deflated;
  my $orig_inflated = $re->original_inflated;
  my @filters = $re->original_filters;
  my $new_deflated = $re->refilter(@filters);
  my $new_png = $re->as_png;


This module offers low-level access to PNG image data. The primary purpose is to support rewriting IDAT chunks which hold the main image data. IDAT chunks can be coalesced, re-compressed, and the filters applied to each scanline in the image to support compression can be retrieved and changed. Modified data can be serialized to a new PNG image, leaving unmodified data intact.


my $rw = Image::PNG::Rewriter->new(%options)

Constructs a new Image::PNG::Rewriter object. The possible options are a required handle parameter which must be an opened handle that can be read from; it is expected that the handle is in binary mode. Note that open supports turning strings into a handle.

The other parameter is zlib which must be a reference to a subroutine that takes some byte string and returns a deflated representation of it that follows the RFC 1950 "zlib" format (neither a raw RFC 1951 stream nor a RFC 1952 "gzip" stream are allowed). The default value uses Compress::Zlib with default settings. The routine must not modify its input.


Returns the width specified in the IHDR chunk.


Returns the height specified in the IHDR chunk.


Returns the depth specified in the IHDR chunk.


Returns the color mode specified in the IHDR chunk.


Returns a list of hash references containing the keys type, size, data, and crc32, containing, respectively, the type of the chunk, the size of the data in the chunk in bytes, the raw data contained in the chunk, and the CRC32 checksum in the original chunk.


The raw RFC 1950-encoded coalesced data of all IDAT chunks in the image.


As $re->original_deflated but inflated.


A list containing the filters applied to the scanlines in the image. The length is equivalent to the height of the image.


Returns a byte string representing the original image except that all original IDAT chunks are replaced by a single IDAT chunk with any changes that may have been applied to the data.

my ($deflated, $inflated) = $re->refilter(1,2,3,...)

Re-encodes the image data by removing the filter that had been applied to the input and applies the new filters. The number of filters must be equal to the number of scanlines in the image (the image's height). Only supports filters 0 through 4. Returns a list containing the deflated and inflated date after applying the new filters. The values should not be modified.


Returns the number of bytes contained within each scanline. The size of the raw image data is this value times the image's height. Note that each scanline starts with a byte that identifies the filter that has been applied to it.


Returns the number of bytes between two "corresponding" values on a scanline. The number of bytes per pixel or 1 if pixels occupy less space than a byte.



  Copyright (c) 2011 Bjoern Hoehrmann <>.
  This module is licensed under the same terms as Perl itself.