PerlMagick Cropping Help

H

hoyoul

Hi,

I am using ImageMagick 6.2.0.7-2 and PerlMagick to create thumbnails
from images. Basically I am reading in an image with ImageMagick,
resizing it so either the width or the height is 100 pixels (depending
on which is smaller) and then cropping it to 100 by 100 pixels. So for
instance, I have an image that is 325 by 500 pixels. After the process
above, instead of ending up with a thumbnail that's 100 by 100 pixels
(centered), I get an image that's 100 by 154 (basically resized from
325 by 500) and the top and bottom of the image is white while the
middle 100 by 100 is the image. So instead of cropping the image, it's
just turning the cropped areas into a white background. Any clues as to
why? Here is the code:

sub createThumbnail {
my $this = shift;

if (!$this->{'image'} || !$this->{'size'}) { return; }

my $image = Image::Magick->new;
#$image->Read(blob => $this->{'image'});
$image->BlobToImage($this->{'image'});

my ($width, $height) = $image->Get('width', 'height');
my $offsetx = 0;
my $offsety = 0;

# Resize image before attempting to crop the image
if ($width <= $height) {
my $h = $this->round(($height / $width) * $this->{'_thumb_width'});
$offsety = $this->round(($h - $this->{'_thumb_height'}) / 2);
$image->Resize(width => $this->{'_thumb_width'}, height => $h);
}
else {
my $w = $this->round(($width / $height) *
$this->{'_thumb_height'});
$offsetx = $this->round(($w - $this->{'_thumb_width'}) / 2);
#$image->Resize(width => $w, height => $this->{'_thumb_height'});
$image->Resize(geometry => $w.'x'.$this->{'_thumb_height'});
}

# Croping image with a re-adjusted offset
$image->Crop(
width => $this->{'_thumb_width'},
height => $this->{'_thumb_height'},
x => $offsetx, y => $offsety);

print "Content-type: image/gif\n\n";
print $image->ImageToBlob;
exit;
}

I tried $image->Chop but that function doesn't seem to work at all with
my version. Any help would greatly be appreciated!

Thanks
Hoyoul
 
K

ko

Hi,

I am using ImageMagick 6.2.0.7-2 and PerlMagick to create thumbnails
from images. Basically I am reading in an image with ImageMagick,
resizing it so either the width or the height is 100 pixels (depending
on which is smaller) and then cropping it to 100 by 100 pixels. So for
instance, I have an image that is 325 by 500 pixels. After the process
above, instead of ending up with a thumbnail that's 100 by 100 pixels
(centered), I get an image that's 100 by 154 (basically resized from
325 by 500) and the top and bottom of the image is white while the
middle 100 by 100 is the image. So instead of cropping the image, it's
just turning the cropped areas into a white background. Any clues as to
why? Here is the code:

sub createThumbnail {
my $this = shift;

if (!$this->{'image'} || !$this->{'size'}) { return; }

my $image = Image::Magick->new;
#$image->Read(blob => $this->{'image'});
$image->BlobToImage($this->{'image'});

my ($width, $height) = $image->Get('width', 'height');
my $offsetx = 0;
my $offsety = 0;

# Resize image before attempting to crop the image
if ($width <= $height) {
my $h = $this->round(($height / $width) * $this->{'_thumb_width'});
$offsety = $this->round(($h - $this->{'_thumb_height'}) / 2);
$image->Resize(width => $this->{'_thumb_width'}, height => $h);
}
else {
my $w = $this->round(($width / $height) *
$this->{'_thumb_height'});
$offsetx = $this->round(($w - $this->{'_thumb_width'}) / 2);
#$image->Resize(width => $w, height => $this->{'_thumb_height'});
$image->Resize(geometry => $w.'x'.$this->{'_thumb_height'});
}

# Croping image with a re-adjusted offset
$image->Crop(
width => $this->{'_thumb_width'},
height => $this->{'_thumb_height'},
x => $offsetx, y => $offsety);

print "Content-type: image/gif\n\n";
print $image->ImageToBlob;
exit;
}

I tried $image->Chop but that function doesn't seem to work at all with
my version. Any help would greatly be appreciated!

Thanks
Hoyoul

Try passing 100 to Crop() for width and height instead of the
'_thumb_width' and '_thumb_height' attributes.

Or you could set another attribute like 'crop_size' => '100x100' for
the desired size and do:

$image->Crop(geometry => $this->{crop_size},
x => $offsetx, y => $offsety);

HTH - keith
 
H

hoyoul

Hi Keith,

I tried with actual numbers as such:

$image->Crop(geometry => '100x100', x => 0, y => 27);

and:

$image->Crop(geometry => '100x100+0+27');

Still no luck. Same results, with the image remaining 100x154 and the
top and bottom with a white margin or 27 pixels wide.

-Hoyoul
 
H

hoyoul

I discovered another thing, this only occurs with GIF file formats,
PNG, JPG/JPEG are cropped correctly. Is there something I need to set
for GIF format files to behave correctly? I tried a variety of GIF
files and no luck.
 
H

hoyoul

Well if anybody runs into this problem, my solution was to basically
convert the image into something other than GIF and then crop it. If
you absolutely want it GIF, you can convert it back afterwards.

$image->Set(magick => 'JPG');
$image->Crop(geometry => 'some geometry');
$image->Set(magick => 'GIF');

Might be a bug on how Cropping behaves for GIF formats...

Regards,
Hoyoul
 
K

ko

For whatever reason, I didn't notice you were using gifs. Try adding
this to your existing code:

$image->Set(page => '+0+0');

Call the method *after* Crop()

HTH -keith
 
K

ko

For whatever reason, I didn't notice you were using gifs. Try adding
this to your existing code:

$image->Set(page => '+0+0');

Call the method *after* Crop()

HTH -keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top