Imager::Filters(3pm) | User Contributed Perl Documentation | Imager::Filters(3pm) |
Imager::Filters - Entire Image Filtering Operations
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);
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;
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 autolevels_skew 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 gaussian2 stddevX stddevY gradgen xo yo colors dist 0 hardinvert hardinvertall 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:
# 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>.
# 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.
$img->filter(type=>"bumpmap", bump=>$bumpmap_img, lightx=>10, lighty=>10, st=>5) or die $img->errstr;
$img->filter(type=>"bumpmap_complex", bump=>$bumpmap_img) or die $img->errstr;
# 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;
# 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;
The "repeat" option controls how the fill is repeated for some "ftype"s after it leaves the AB range:
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:
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:
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;
http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node99.html
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;
http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node99.html
Values of "stddevX" or "stddevY" around 0.5 provide a barely noticeable blur, values around 5 provide 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;
$img->filter(type="gradgen", xo=>[ 10, 50, 10 ], yo=>[ 10, 50, 50 ], colors=>[ qw(red blue green) ]);
$img->filter(type=>"hardinvert") or die $img->errstr;
$img->filter(type=>"hardinvertall") or die $img->errstr;
$img->filter(type=>"mosaic", size=>5) or die $img->errstr;
# 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;
$img->filter(type=>"radnoise", xo=>50, yo=>50, ascale=>1, rscale=>0.02) or die $img->errstr;
$img->filter(type=>"postlevels", levels=>10) or die $img->errstr;
$img->filter(type=>"turbnoise", xo=>10, yo=>10, scale=>10) or die $img->errstr;
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)
eg.
$img->filter(type=>"unsharpmask", stddev=>1, scale=>0.5) or die $img->errstr;
"unsharpmark" has the following parameters:
$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
As of Imager 0.48 you can create perl or XS based filters and hook them into 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:
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.
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:
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.
Accepts a single parameter, the name of a shared library file to load.
Returns true on success. Check Imager->errstr on failure.
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):
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.
Parameters:
For large sample images this is scaled down to the range 0 .. 1.
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:
Arnar M. Hrafnkelsson, Tony Cook <tonyc@cpan.org>.
Imager, Imager::Filter::Flines, Imager::Filter::Mandelbrot
$Revision$
2023-01-11 | perl v5.36.0 |