Ask a Question related to PERL Modules, Design and Development.
-
Lars Haugseth #1
Image::Magick and padding of image
I need to pad an image with a white border to make it fit a certain
size. So far I've found the Border() method to do the job, but not
quite. As far as I can see, it adds the same amount of padding to
the right of the image as it does to the left, and the same amount
of padding to the top as to the bottom. This means I can never pad
an image of size 393x297 so that it becomes 400x300 exactly, as the
geometry parameter works on integers only. I realise I can add some
extra border and crop the image afterwards, but want to know if
there's a way to do what I wish in one operation. Something
equivalent to Netpbm's "pnmpad -[left|right|top|bottom]" would be
nice.
Below is my current test code:
- - -
#!/usr/bin/perl -w
use Image::Magick;
use strict;
resizeImage('foo.jpg', 'bar.jpg', 400, 300);
sub resizeImage {
my ($sourceFile, $targetFile, $w, $h) = @_;
my $image = Image::Magick->new();
# Read image from source file
$image->Read($sourceFile);
# Scale image if too large
$image->Scale(geometry => "${w}x${h}>");
my ($sw, $sh) = ($image->Get('columns'), $image->Get('rows'));
# Pad image if needed
if ($sw < $w || $sh < $h) {
# Calculate horizontal and vertical padding
my ($horz, $vert) = (int(($w - $sw + 1)/2), int(($h - $sh + 1)/2));
$image->Border(geometry => "${horz}x${vert}", fill => 'white');
my ($pw, $ph) = ($image->Get('columns'), $image->Get('rows'));
# Crop image if needed
if ($pw > $w || $ph > $h) {
$image->Crop(width => $w, height => $h);
}
}
# Write image to target file
$image->Write($targetFile);
}
- - -
--
Lars Haugseth
Lars Haugseth Guest
-
Errror in mod Image::magick or something else?
Hi, I've bought a perl script a few days ago, which will not work at all. The programmer can't or will not help me in solving the problem.... -
Image::Magick newbie
Hi, Hopefully, this will be easy for someone else: perl Makefile.PL LIB=xx PREFIX=xx went off okay. but.. Magick.xs was throwing up a lot... -
Help: Image::Magick::Thumbnail
Hi, The following example (from cpan.org) take an .jpg image and creates a thumbnail. It creates an image where the biggest size is 50 pixels. ... -
Image Magick
Mike At Spy wrote: the easiest way to remove them is to use RPM, if that was what was used to install them rpm -e ImageMagick if there are no... -
[PHP] Image Magick
Mike -- ...and then Mike At Spy said... % % Anyone here familiar with the installation of Image Magick and what filesit % installs on your...



Reply With Quote

