PerlMagick Cropping Help

Discussion in 'Perl Misc' started by hoyoul@gmail.com, Apr 21, 2005.

  1. Guest

    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
    , Apr 21, 2005
    #1
    1. Advertising

  2. ko Guest

    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
    ko, Apr 21, 2005
    #2
    1. Advertising

  3. Guest

    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
    , Apr 21, 2005
    #3
  4. Guest

    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.
    , Apr 21, 2005
    #4
  5. Guest

    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
    , Apr 21, 2005
    #5
  6. ko Guest

    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
    ko, Apr 22, 2005
    #6
  7. ko Guest

    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
    ko, Apr 22, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FrankV
    Replies:
    0
    Views:
    376
    FrankV
    Nov 8, 2005
  2. Andrew Neiderer

    JAI example of cropping

    Andrew Neiderer, Sep 14, 2005, in forum: Java
    Replies:
    2
    Views:
    9,988
    Martin Trobec
    Sep 17, 2005
  3. Darrel

    Cropping an image

    Darrel, Apr 23, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    478
    Kevin Spencer
    Apr 23, 2006
  4. Kamus of Kadizhar

    Cropping log files

    Kamus of Kadizhar, Dec 16, 2003, in forum: Python
    Replies:
    6
    Views:
    313
    Gerrit Holl
    Dec 17, 2003
  5. JJ
    Replies:
    3
    Views:
    705
    PJ on Development
    Nov 19, 2007
Loading...

Share This Page