Cropping -- HowTo

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
mcm1303

Cropping -- HowTo

Post by mcm1303 »

I was having problems cropping, after 5 hours finally figured it out and thought I'd post the answer here to benefit others.

The Scenario:
1. We can succesfully read the image:

$image = new Image::Magick;
$result = $image->Read(filename=>$sourceDir . $sourceFile);

2. We can then crop:
$image = $image->Crop($finalSizeX . "x" . $finalSizeY . "+" . $offsetX . "+" . $offsetY);

...But when we write the image out:
$image = $image->Write($destDir . $destFile);

We get an image that is sized exactly the same size as the source but we find that all the cropping had done is to blank out the pixels around our crop location. So if we had cropped the lower right corner the entire left side is blank and the entire top side is blank. Only a partial solution in my opinion.

The solution is to do a page after your crop (which I believe is the perl equivalent to repage in the command-line version):
$image->Set(page=>'+0+0');

Then simply write it out as shown above and you will have a new image sized the same size as your crop size.

Hope this helps someone somewhere.

Marlin
Post Reply