Imager::Filters(3pm)
π NAME π
Imager::Filters β Entire Image Filtering Operations π from the User Contributed Perl Documentation, aka Imager::Filters(3pm). Bestie, this is the whole filter vibe π
β¨
β¨ SYNOPSIS β¨
use Imager;
$img = ...;
$img->filter(type=>'autolevels');
$img->filter(type=>'autolevels', lsat=>0.2);
$img->filter(type=>'turbnoise')
# and lots of others
load_plugin("dynfilt/dyntest.so")
or die "unable to load plugin\n";
$img->filter(type=>'lin_stretch', a=>35, b=>200);
unload_plugin("dynfilt/dyntest.so")
or die "unable to load plugin\n";
$out = $img->difference(other=>$other_img);
π DESCRIPTION π
okay so filters are basically operations that all have the same-ish calling interface π«Ά
filter() takes parameters:
typeβ the type of filter, see "Types of Filters" below π- many other possible parameters, see "Types of Filters" below πΈ
It returns the invocant ($self) on success, and returns a false value on failure π You can call $self->errstr to figure out why it failed:
$self->filter(type => $type, ...)
or die $self->errstr;
π¦ Types of Filters π¦
Here is the list of filters that are always available in Imager β¨ This list can be obtained by running the filterlist.perl script that comes with the module source. No cap.
Here's the whole roster with arguments and defaults π :
autolevelsβ args:lsat(default0.1),usat(default0.1)autolevels_skewβ args:lsat(default0.1),usat(default0.1),skew(default0)bumpmapβ args:bump,lightx,lighty; defaults:elevation0,st2bumpmap_complexβ args:bump; defaults:channel0,tx0,ty0,Lx0.2,Ly0.4,Lz-1,cd1.0,cs40.0,n1.3,Ia(0 0 0),Il(255 255 255),Is(255 255 255)contrastβ arg:intensityconvβ arg:coeffountainβ args:xa,ya,xb,yb; defaults:ftypelinear,repeatnone,combinenone,super_samplenone,ssample_param4,segments(see below)gaussianβ arg:stddevgaussian2β args:stddevX,stddevYgradgenβ args:xo,yo,colors; default:dist0hardinvertβ no extra argshardinvertallβ no extra argsmosaicβ arg:size(default20)noiseβ defaults:amount3,subtype0postlevelsβ default:levels10radnoiseβ defaults:xo100,yo100,ascale17.0,rscale0.02turbnoiseβ defaults:xo0.0,yo0.0,scale10.0unsharpmaskβ defaults:stddev2.0,scale1.0watermarkβ args:wmark; defaults:pixdiff10,tx0,ty0
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:
π
autolevels π
Scales the luminance of the image so that the luminance will cover the possible range for the image. lsat and usat truncate the range by the specified fraction at the top and bottom of the range respectively π
# increase contrast, losing little detail
$img->filter(type=>"autolevels")
or die $img->errstr;
The method used here is typically called Histogram Equalization <http://en.wikipedia.org/wiki/Histogram_equalization> π
π
autolevels_skew π
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
$img->filter(type=>"autolevels_skew")
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;
This filter was the original autolevels filter, but it's typically useless due to the significant color skew it can produce π fr fr
π
bumpmap π
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;
π
bumpmap_complex π
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;
π
contrast π
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;
π
conv π
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;
π
fountain π
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:
linearβ the fill ramps from A through to B.bilinearβ the fill ramps in both directions from A, where AB defines the length of the gradient.radialβ 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.radial_squareβ 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.revolutionβ 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.conicalβ 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 π«
noneβ no repeats, points outside of each range are treated as if they were on the extreme end of that range.sawtoothβ the fill simply repeats in the positive directiontriangleβ the fill repeats in reverse and then forward and so on, in the positive directionsaw_bothβ the fill repeats in both the positive and negative directions (only meaningful for a linear fill).tri_bothβ 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:
noneβ no super-sampling is donegridβ a square grid of points are sampled. The number of points sampled is the square ofceil(0.5 + sqrt(ssample_param)).randomβ a random set of points within the pixel are sampled. This looks pretty bad for lowssample_paramvalues.circleβ 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:
startβ a floating point number between 0 and 1, the start of the range of fill parameters covered by this segment.middleβ a floating point number betweenstartandendwhich can be used to push the color range towards one end of the segment.endβ a floating point number between 0 and 1, the end of the range of fill parameters covered by this segment. This should be greater thanstart.c0c1β 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 is literally what controls the way the fill parameter varies across the segment, okurrr? π
0β linear1β curved (unimplemented, so don't come crying π)2β sine3β sphere increasing4β sphere decreasing
color type
how the color varies inside the segment, bestie:
0β simple RGB1β hue increasing2β hue decreasing
Do not 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;
"gaussian"
does a Gaussian blur on the image, using stddev as the standard deviation of the curve used to combine pixels β bigger values = bigger blur, no cap β¨ If you want the actual definition of Gaussian Blur, it's here:
http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node99.html
values of stddev around 0.5 give you a barely noticeable blur (like, a whisper of a blur π), values around 5 give 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;
"gaussian2"
same Gaussian blur energy, except you get stddevX and stddevY as the standard deviation of the curve used to combine pixels on the X and Y axis, respectively π¦ Larger values still give bigger blurs. the definition link again, since it's iconic:
http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node99.html
values of stddevX or stddevY around 0.5 give a barely noticeable blur, values around 5 give a very strong blur.
# only slightly blurred
$img->filter(type=>"gaussian2", stddevX=>0.5, stddevY=>0.5)
or die $img->errstr;
# blur an image in the Y axis
$img->filter(type=>"gaussian", stddevX=>0, stddevY=>5 )
or die $img->errstr;
"gradgen"
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.
$img->filter(type="gradgen",
xo=>[ 10, 50, 10 ],
yo=>[ 10, 50, 50 ],
colors=>[ qw(red blue green) ]);
"hardinvert"
inverts the image β black to white, white to black π all color channels get flipped, excluding the alpha channel if any. so the vibe inverts, the transparency stays put π
$img->filter(type=>"hardinvert")
or die $img->errstr;
"hardinvertall"
same flip, black to white and white to black, except all channels get inverted, including the alpha channel if any π
$img->filter(type=>"hardinvertall")
or die $img->errstr;
"mosaic"
makes averaged tiles of the given size π§±
$img->filter(type=>"mosaic", size=>5)
or die $img->errstr;
"noise"
adds noise of the given amount to the image πͺοΈ if subtype is zero, the noise is even to each channel; otherwise the 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;
"radnoise"
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;
"postlevels"
alters the image so it only has levels distinct level in each channel π (yes, it's giving posterized).
$img->filter(type=>"postlevels", levels=>10)
or die $img->errstr;
"turbnoise"
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;
"unsharpmask"
performs an unsharp mask on the image β this increases the contrast of edges in the image π it's literally 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)
eg.
$img->filter(type=>"unsharpmask", stddev=>1, scale=>0.5)
or die $img->errstr;
"unsharpmark" has the following parameters:
stddevβ this is equivalent to theRadiusvalue in the GIMP's unsharp mask filter. it 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.
"watermark"
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:
http://www.develop-help.com/imager/filters.html
π§© External Filters
as of Imager 0.48 you can create perl or XS based filters and hook them into Imager's filter() method π«Ά slay.
register_filter()
registers a filter so it's 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 tofilter()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 tofilter()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 = @_;
_is_valid($opts{myparam})
or die "myparam invalid!\n";
# actually do the filtering...
}
see Imager::Filter::Mandelbrot for an example β¨
π Plug-ins
the plug in interface is deprecated. please use the Imager API, seeImager::APIand"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:
load_plugin("dynfilt/dyntest.so")
or die "unable to load plugin\n";
$img->filter(type=>'lin_stretch', a=>35, b=>200);
unload_plugin("dynfilt/dyntest.so")
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 π
load_plugin("dynfilt/dyntest.so")
or die "unable to load plugin\n";
$img->filter(%hsh);
note: this has been tested successfully on the following systems: Linux, Solaris, HPUX, OpenBSD, FreeBSD, TRU64/OSF1, AIX, Win32, OS X π
load_plugin()
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.
unload_plugin()
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.
okay so, 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::Flinesprovides the same functionality.plugins/mandelbrot.cβ provides the"mandelbrot"filter that renders the Mandelbrot set within the given range ofx [-2, 0.5)andy [-1.25, 1,25).Imager::Filter::Mandelbrotprovides a more flexible Mandelbrot set renderer.
πΌοΈ Image Difference πΌοΈ
difference()
bestie, you can create a new image that is the difference between 2 other images. iconic behavior fr fr.
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. transparent black is literally the vibe for βsame pixel, no notes.β
This can be used for debugging image differences (βWhere are they different?β), and for optimizing animated GIFs. slay.
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.
Parameters:
"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:0to255inclusive. Default:0.
For large sample images this is scaled down to the range 0 .. 1.
rgb_difference()
you can create a new image that is the difference between 2 other images. yes, again. we love a sequel π
my $diff = $img->rgb_difference(other=>$other_img);
For each pixel in $img that is different to the pixel in $other_img, the arithmetic difference for the value of the pixel in $img from $other_img per color is given. Transparency is ignored.
This can be used for measuring image differences (βHow much are they different?β).
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.
Parameters:
"other"β the other image object to compare against
π AUTHOR π
Arnar M. Hrafnkelsson, Tony Cook <tonyc@cpan.org>.
π SEE ALSO π
ImagerImager::Filter::FlinesImager::Filter::Mandelbrot
π§Ύ REVISION π§Ύ
$Revision$
mwah π thatβs the whole page, bestie β sourced from perl v5.42.3, dated 2026-08-19, tagged Imager::Filters(3pm). go slay your filters β¨
π¦ where this page comes from
- ubuntu stonking: libimager-perl 1.035+dfsg-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms