Question

I have a PNG image what is generated by convert from a PDF file. This PNG is letter size and transparent except the content part.

I use the Image::Magick perl module to manipulate images (like a newbie). The content start at 28x28px and the width is constant. The height is variable.

How can I cut the transparent part of the image and get the content only? Or how can I find the last not-transparent line with Image::Magick?

Était-ce utile?

La solution

You want to use the Trim() method, followed by resetting the page attribute. Trim will crop off all of the image that is exactly the same color as the corner pixels (in your case transparent). Resetting the page attribute will make sure your content is properly aligned on the new, smaller image canvas.

Here's some more info on Trim() from the documentation for ImageMagick: http://www.imagemagick.org/script/command-line-options.php#trim

And here it is in the list of valid image manipulation methods in PerlMagick (though the documentation here is a bit sparser): http://www.imagemagick.org/script/perl-magick.php#manipulate

Something like the following should do the trick for you:

use strict;
use Image::Magick;

my $in = $ARGV[0];
my $out = $ARGV[1];

my $transparent_png = Image::Magick->new;
$transparent_png->Read("$in");
$transparent_png->Trim();
$transparent_png->Set(page=>'0x0+0+0');
$transparent_png->Write("$out");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top