Imager::Trim - automatic cropping for images using Imager.
version 0.007
use Imager::Trim; my $imager = Imager::Trim->new( file => 'image.jpg' ) or die "Cannot open file: ", Imager::Trim->errstr(); my $cropped_image = $imager->trim( fuzz => 50 ); $cropped_image->write( file => 'cropped_image.jpg' );
By default the first pixel from top left is used for automatic cropping, however you can provide yourself an Imager::Color for custom action:
Imager::Color
use Imager::Trim; use Imager::Color; my $white_color = Imager::Color->new("#FFFFFF"); my $imager = Imager::Trim->new( file => 'image_with_white_background.jpg' ); my $color_cropped_image = $imager->trim( color => $white_color );
You can even do the cropping manually using Imager (on this example, we're leaving 10px extra space around the automatically cropped image, if possible:
use Imager::Trim; my $imager = Imager::Trim->new( file => 'image.jpg' ); my $cropped_image = $imager->trim(); my $border = 10; my $top = $cropped_image->{trim_top} - $border; my $left = $cropped_image->{trim_left} - $border; my $right = $cropped_image->{trim_right} + $border; my $bottom = $cropped_image->{trim_bottom} + $border; my $manually_cropped_image = $imager->crop( top => ($top > 0) ? $top : 0, left => ($left > 0) ? $left : 0, right => ($right > 0) ? $right : 0, bottom => ($bottom > 0) ? $bottom : 0 );
This module extends Imager to allow automatic cropping of images. The method is similar as used in image editors (e.g. "magic wand") or with ImageMagick's "trim" (e.g. 'convert image.jpg -fuzz 50 -background white -trim cropped_image.jpg').
Imager
http://imager.perl.org/
Jussi Kinnula <spot@cpan.org>
This software is copyright (c) 2016 by Jussi Kinnula.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Imager::Trim, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Imager::Trim
CPAN shell
perl -MCPAN -e shell install Imager::Trim
For more information on module installation, please visit the detailed CPAN module installation guide.