Imager::Filters - Entire Image Filtering Operations


  use Imager;

  $img = ...;

  $img->filter(type=>'autolevels', lsat=>0.2);

  # and lots of others

    or die "unable to load plugin\n";

  $img->filter(type=>'lin_stretch', a=>35, b=>200);

    or die "unable to load plugin\n";

  $out = $img->difference(other=>$other_img);


Filters are operations that have similar calling interface.



Returns the invocant ($self) on success, returns a false value on failure. You can call $self->errstr to determine the cause of the failure.

  $self->filter(type => $type, ...)
    or die $self->errstr;

Types of Filters

Here is a list of the filters that are always available in Imager. This list can be obtained by running the filterlist.perl script that comes with the module source.

  Filter          Arguments   Default value
  autolevels      lsat        0.1
                  usat        0.1
                  skew        0

  bumpmap         bump lightx lighty
                  elevation   0
                  st          2

  bumpmap_complex bump
                  channel     0
                  tx          0
                  ty          0
                  Lx          0.2
                  Ly          0.4
                  Lz          -1 
                  cd          1.0 
                  cs          40.0
                  n           1.3
                  Ia          (0 0 0)
                  Il          (255 255 255)
                  Is          (255 255 255)

  contrast        intensity

  conv            coef

  fountain        xa ya xb yb
                  ftype        linear
                  repeat       none
                  combine      none
                  super_sample none
                  ssample_param 4
                  segments(see below)

  gaussian        stddev

  gradgen         xo yo colors 
                  dist         0



  mosaic          size         20

  noise           amount       3
                  subtype      0

  postlevels      levels       10

  radnoise        xo           100
                  yo           100
                  ascale       17.0
                  rscale       0.02

  turbnoise       xo           0.0
                  yo           0.0
                  scale        10.0

  unsharpmask     stddev       2.0
                  scale        1.0

  watermark       wmark
                  pixdiff      10
                  tx           0
                  ty           0

All parameters must have some value but if a parameter has a default value it may be omitted when calling the filter function.

Every one of these filters modifies the image in place.

If none of the filters here do what you need, the "transform()" in Imager::Engines or "transform2()" in Imager::Engines function may be useful.

A reference of the filters follows:


scales the value of each channel so that the values in the image will cover the whole possible range for the channel. lsat and usat truncate the range by the specified fraction at the top and bottom of the range respectively.

  # increase contrast per channel, losing little detail
    or die $img->errstr;

  # increase contrast, losing 20% of highlight at top and bottom range
  $img->filter(type=>"autolevels", lsat=>0.2, usat=>0.2)
    or die $img->errstr;

uses the channel elevation image bump as a bump map on your image, with the light at (lightx, lightty), with a shadow length of st.

  $img->filter(type=>"bumpmap", bump=>$bumpmap_img,
               lightx=>10, lighty=>10, st=>5)
    or die $img->errstr;

uses the channel channel image bump as a bump map on your image. If Lz < 0 the three L parameters are considered to be the direction of the light. If Lz > 0 the L parameters are considered to be the light position. Ia is the ambient color, Il is the light color, Is is the color of specular highlights. cd is the diffuse coefficient and cs is the specular coefficient. n is the shininess of the surface.

  $img->filter(type=>"bumpmap_complex", bump=>$bumpmap_img)
    or die $img->errstr;

scales each channel by intensity. Values of intensity < 1.0 will reduce the contrast.

  # higher contrast
  $img->filter(type=>"contrast", intensity=>1.3)
    or die $img->errstr;

  # lower contrast
  $img->filter(type=>"contrast", intensity=>0.8)
    or die $img->errstr;

performs 2 1-dimensional convolutions on the image using the values from coef. coef should be have an odd length and the sum of the coefficients must be non-zero.

  # sharper
  $img->filter(type=>"conv", coef=>[-0.5, 2, -0.5 ])
    or die $img->errstr;

  # blur
  $img->filter(type=>"conv", coef=>[ 1, 2, 1 ])
    or die $img->errstr;

  # error
  $img->filter(type=>"conv", coef=>[ -0.5, 1, -0.5 ])
    or die $img->errstr;

renders a fountain fill, similar to the gradient tool in most paint software. The default fill is a linear fill from opaque black to opaque white. The points A(Cxa, ya) and B(xb, yb) control the way the fill is performed, depending on the ftype parameter:


the fill ramps from A through to B.


the fill ramps in both directions from A, where AB defines the length of the gradient.


A is the center of a circle, and B is a point on it's circumference. The fill ramps from the center out to the circumference.


A is the center of a square and B is the center of one of it's sides. This can be used to rotate the square. The fill ramps out to the edges of the square.


A is the center of a circle and B is a point on its circumference. B marks the 0 and 360 point on the circle, with the fill ramping clockwise.


A is the center of a circle and B is a point on it's circumference. B marks the 0 and point on the circle, with the fill ramping in both directions to meet opposite.

The repeat option controls how the fill is repeated for some ftypes after it leaves the AB range:


no repeats, points outside of each range are treated as if they were on the extreme end of that range.


the fill simply repeats in the positive direction


the fill repeats in reverse and then forward and so on, in the positive direction


the fill repeats in both the positive and negative directions (only meaningful for a linear fill).


as for triangle, but in the negative direction too (only meaningful for a linear fill).

By default the fill simply overwrites the whole image (unless you have parts of the range 0 through 1 that aren't covered by a segment), if any segments of your fill have any transparency, you can set the combine option to 'normal' to have the fill combined with the existing pixels. See the description of combine in Imager::Fill.

If your fill has sharp edges, for example between steps if you use repeat set to 'triangle', you may see some aliased or ragged edges. You can enable super-sampling which will take extra samples within the pixel in an attempt anti-alias the fill.

The possible values for the super_sample option are:


no super-sampling is done


a square grid of points are sampled. The number of points sampled is the square of ceil(0.5 + sqrt(ssample_param)).


a random set of points within the pixel are sampled. This looks pretty bad for low ssample_param values.


the points on the radius of a circle within the pixel are sampled. This seems to produce the best results, but is fairly slow (for now).

You can control the level of sampling by setting the ssample_param option. This is roughly the number of points sampled, but depends on the type of sampling.

The segments option is an arrayref of segments. You really should use the Imager::Fountain class to build your fountain fill. Each segment is an array ref containing:


a floating point number between 0 and 1, the start of the range of fill parameters covered by this segment.


a floating point number between start and end which can be used to push the color range towards one end of the segment.


a floating point number between 0 and 1, the end of the range of fill parameters covered by this segment. This should be greater than start.


The colors at each end of the segment. These can be either Imager::Color or Imager::Color::Float objects.

segment type

The type of segment, this controls the way the fill parameter varies over the segment. 0 for linear, 1 for curved (unimplemented), 2 for sine, 3 for sphere increasing, 4 for sphere decreasing.

color type

The way the color varies within the segment, 0 for simple RGB, 1 for hue increasing and 2 for hue decreasing.

Don't forget to use Imager::Fountain instead of building your own. Really. It even loads GIMP gradient files.

  # build the gradient the hard way - linear from black to white,
  # then back again
  my @simple =
     [   0, 0.25, 0.5, 'black', 'white', 0, 0 ],
     [ 0.5. 0.75, 1.0, 'white', 'black', 0, 0 ],
  # across
  my $linear = $img->copy;
  $linear->filter(type     => "fountain",
                  ftype    => 'linear',
                  repeat   => 'sawtooth',
                  segments => \@simple,
                  xa       => 0,
                  ya       => $linear->getheight / 2,
                  xb       => $linear->getwidth - 1,
                  yb       => $linear->getheight / 2)
    or die $linear->errstr;
  # around
  my $revolution = $img->copy;
  $revolution->filter(type     => "fountain",
                      ftype    => 'revolution',
                      segments => \@simple,
                      xa       => $revolution->getwidth / 2,
                      ya       => $revolution->getheight / 2,
                      xb       => $revolution->getwidth / 2,
                      yb       => 0)
    or die $revolution->errstr;
  # out from the middle
  my $radial = $img->copy;
  $radial->filter(type     => "fountain",
                  ftype    => 'radial',
                  segments => \@simple,
                  xa       => $im->getwidth / 2,
                  ya       => $im->getheight / 2,
                  xb       => $im->getwidth / 2,
                  yb       => 0)
    or die $radial->errstr;

performs a Gaussian blur of the image, using stddev as the standard deviation of the curve used to combine pixels, larger values give bigger blurs. For a definition of Gaussian Blur, see:

Values of stddev around 0.5 provide a barely noticeable blur, values around 5 provide a very strong blur.

  # only slightly blurred
  $img->filter(type=>"gaussian", stddev=>0.5)
    or die $img->errstr;

  # more strongly blurred
  $img->filter(type=>"gaussian", stddev=>5)
    or die $img->errstr;

renders a gradient, with the given colors at the corresponding points (x,y) in xo and yo. You can specify the way distance is measured for color blending by setting dist to 0 for Euclidean, 1 for Euclidean squared, and 2 for Manhattan distance.

               xo=>[ 10, 50, 10 ], 
               yo=>[ 10, 50, 50 ],
               colors=>[ qw(red blue green) ]);

inverts the image, black to white, white to black. All color channels are inverted, excluding the alpha channel if any.

    or die $img->errstr;

inverts the image, black to white, white to black. All channels are inverted, including the alpha channel if any.

    or die $img->errstr;

produces averaged tiles of the given size.

  $img->filter(type=>"mosaic", size=>5)
    or die $img->errstr;

adds noise of the given amount to the image. If subtype is zero, the noise is even to each channel, otherwise noise is added to each channel independently.

  # monochrome noise
  $img->filter(type=>"noise", amount=>20, subtype=>0)
    or die $img->errstr;

  # color noise
  $img->filter(type=>"noise", amount=>20, subtype=>1)
    or die $img->errstr;

renders radiant Perlin turbulent noise. The center of the noise is at (xo, yo), ascale controls the angular scale of the noise , and rscale the radial scale, higher numbers give more detail.

  $img->filter(type=>"radnoise", xo=>50, yo=>50,
               ascale=>1, rscale=>0.02)
    or die $img->errstr;

alters the image to have only levels distinct level in each channel.

  $img->filter(type=>"postlevels", levels=>10)
    or die $img->errstr;

renders Perlin turbulent noise. (xo, yo) controls the origin of the noise, and scale the scale of the noise, with lower numbers giving more detail.

  $img->filter(type=>"turbnoise", xo=>10, yo=>10, scale=>10)
    or die $img->errstr;

performs an unsharp mask on the image. This increases the contrast of edges in the image.

This is the result of subtracting a Gaussian blurred version of the image from the original. stddev controls the stddev parameter of the Gaussian blur. Each output pixel is:

  in + scale * (in - blurred)


  $img->filter(type=>"unsharpmask", stddev=>1, scale=>0.5)
    or die $img->errstr;

unsharpmark has the following parameters:

  • stddev - this is equivalent to the Radius value in the GIMP's unsharp mask filter. This controls the size of the contrast increase around edges, larger values will remove fine detail. You should probably experiment on the types of images you plan to work with. Default: 2.0.

  • scale - controls the strength of the edge enhancement, equivalent to Amount in the GIMP's unsharp mask filter. Default: 1.0.


applies wmark as a watermark on the image with strength pixdiff, with an origin at (tx, ty)

  $img->filter(type=>"watermark", tx=>10, ty=>50, 
               wmark=>$wmark_image, pixdiff=>50)
    or die $img->errstr;

A demonstration of most of the filters can be found at:

External Filters

As of Imager 0.48 you can create perl or XS based filters and hook them into Imager's filter() method:


Registers a filter so it is visible via Imager's filter() method.

  Imager->register_filter(type => 'your_filter',
                          defaults => { parm1 => 'default1' },
                          callseq => [ qw/image parm1/ ],
                          callsub => \&your_filter);
  $img->filter(type=>'your_filter', parm1 => 'something');

The following parameters are needed:

  • type - the type value that will be supplied to filter() to use your filter.

  • defaults - a hash of defaults for the filter's parameters

  • callseq - a reference to an array of required parameter names.

  • callsub - a code reference called to execute your filter. The parameters passed to filter() are supplied as a list of parameter name, value ... which can be assigned to a hash.

    The special parameters image and imager are supplied as the low level image object from $self and $self itself respectively.

    The function you supply must modify the image in place.

    To indicate an error, die with an error message followed by a newline. filter() will store the error message as the errstr() for the invocant and return false to indicate failure.

      sub my_filter {
        my %opts = @_;
          or die "myparam invalid!\n";
        # actually do the filtering...

See Imager::Filter::Mandelbrot for an example.


The plug in interface is deprecated. Please use the Imager API, see Imager::API and "External Filters" for details

It is possible to add filters to the module without recompiling Imager itself. This is done by using DSOs (Dynamic shared object) available on most systems. This way you can maintain your own filters and not have to have it added to Imager, or worse patch every new version of Imager. Modules can be loaded AND UNLOADED at run time. This means that you can have a server/daemon thingy that can do something like:

    or die "unable to load plugin\n";

  $img->filter(type=>'lin_stretch', a=>35, b=>200);

    or die "unable to load plugin\n";

Someone decides that the filter is not working as it should - dyntest.c can be modified and recompiled, and then reloaded:

    or die "unable to load plugin\n";


Note: This has been tested successfully on the following systems: Linux, Solaris, HPUX, OpenBSD, FreeBSD, TRU64/OSF1, AIX, Win32, OS X.


This is a function, not a method, exported by default. You should import this function explicitly for future compatibility if you need it.

Accepts a single parameter, the name of a shared library file to load.

Returns true on success. Check Imager->errstr on failure.


This is a function, not a method, which is exported by default. You should import this function explicitly for future compatibility if you need it.

Accepts a single parameter, the name of a shared library to unload. This library must have been previously loaded by load_plugin().

Returns true on success. Check Imager->errstr on failure.

A few example plug-ins are included and built (but not installed):

  • plugins/dyntest.c - provides the null (no action) filter, and lin_stretch filters. lin_stretch stretches sample values between a and b out to the full sample range.

  • plugins/dt2.c - provides the html_art filter that writes the image to the HTML fragment file supplied in fname as a HTML table.

  • plugins/flines.c - provides the flines filter that dims alternate lines to emulate an old CRT display. Imager::Filter::Flines provides the same functionality.

  • plugins/mandelbrot.c - provides the mandelbrot filter that renders the Mandelbrot set within the given range of x [-2, 0.5) and y [-1.25, 1,25). Imager::Filter::Mandelbrot provides a more flexible Mandelbrot set renderer.

Image Difference


You can create a new image that is the difference between 2 other images.

  my $diff = $img->difference(other=>$other_img);

For each pixel in $img that is different to the pixel in $other_img, the pixel from $other_img is given, otherwise the pixel is transparent black.

This can be used for debugging image differences ("Where are they different?"), and for optimizing animated GIFs.

Note that $img and $other_img must have the same number of channels. The width and height of $diff will be the minimum of each of the width and height of $img and $other_img.


  • other - the other image object to compare against

  • mindist - the difference between corresponding samples must be greater than mindist for the pixel to be considered different. So a value of zero returns all different pixels, not all pixels. Range: 0 to 255 inclusive. Default: 0.

    For large sample images this is scaled down to the range 0 .. 1.


Arnar M. Hrafnkelsson, Tony Cook <>.


Imager, Imager::Filter::Flines, Imager::Filter::Mandelbrot