Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > PerlMagick Cropping Help

Reply
Thread Tools

PerlMagick Cropping Help

 
 
hoyoul@gmail.com
Guest
Posts: n/a
 
      04-21-2005
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

 
Reply With Quote
 
 
 
 
ko
Guest
Posts: n/a
 
      04-21-2005
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
 
 
 
hoyoul@gmail.com
Guest
Posts: n/a
 
      04-21-2005
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

 
Reply With Quote
 
hoyoul@gmail.com
Guest
Posts: n/a
 
      04-21-2005
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.

 
Reply With Quote
 
hoyoul@gmail.com
Guest
Posts: n/a
 
      04-21-2005
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

 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      04-21-2005
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

 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      04-21-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
several problems with cygwin perl, Activeperl, perlmagick and Apache (Windows) Daniela Duerbeck Perl Misc 1 12-21-2005 11:20 PM
PerlMagick, Annotate, and Webdings Aaron Dalton Perl Misc 5 03-27-2005 02:51 AM
perlmagick and image size rossz Perl Misc 7 05-21-2004 06:11 PM
I cann't scale gif with correctly with perlmagick. kenny Perl Misc 7 04-19-2004 09:32 AM
CPANing PerlMagick and it fails Randell D. Perl 0 08-25-2003 04:23 PM



Advertisments