Trim() not working

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

Trim() not working

Post by clark238 »

Help! I'm going crazy... Using 6.4.1 and I can't get Trim() to work. All it does is return the same size image with the background color replaced with transparency. I would hope and want this function to crop the image to 208x104 (size of the bounding box). Here are my input and output files:

input.jpg
Image

output.gif (view image properties to see that it's still 280x280)
Image

Here's my code:

Code: Select all

$image=Image::Magick->new;
$file_input = "input.jpg;
$file_output = "output.gif";
$image->ReadImage($file_input);
unlink($file_input);
$image = $image->[0]; #grab first image in case it's an animation
$image->Trim();
$image->Write($file_output);
undef $image;
If I get the 'bounding-box' property before I do the Trim(), I get "208x104+32+96" and if I do it after the Trim() I get 208x104+0+0

This doesn't work either:

Code: Select all

$box = $image->Get('bounding-box');
$image->Crop(geometry=>$box);
Thanks in advance!
wrnelson55

Re: Trim() not working

Post by wrnelson55 »

I have had success with “Crop” by coding the statement this way.
It requires a little math to get the variable values, but it works for me.
width = width of the cropped area
height = height of the cropped area
x = horizontal offset of the cropped area from the original
y = vertical offset of the cropped area from the original

$i = $image->Crop( width=>$scaleSizeX, height=>$scaleSizeY,
x=>$scaleOffsetX, y=>$scaleOffsetY );
clark238

Re: Trim() not working

Post by clark238 »

$i = $image->Crop( width=>$scaleSizeX, height=>$scaleSizeY, x=>$scaleOffsetX, y=>$scaleOffsetY );
Thanks, but that doesn't work either.

I found and tried Chop() but that behaves like Resize(). I can't find any functions that will allow me to change the canvas size (photoshop term) without affecting the image. I can't Trim, Crop or Chop pixels off the top, sides or bottom. In what version could you do $image->Crop(0x0) ? Maybe I'll revert to that and try it.

This is driving me crazy! what am I doing wrong?

BTW, I'm running perl on Windows if that makes a difference
wrnelson55

Re: Trim() not working

Post by wrnelson55 »

I am running ImageMagick-6.4.1 and the PerlMagick that installed with it on Linux (Mandriva)
sorry that did not work, good luck
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Trim() not working

Post by el_supremo »

I can't find any functions that will allow me to change the canvas size
I don't know Perl but on the command line it is the +repage option and in C you would use the statement:

Code: Select all

	MagickResetImagePage(magick_wand,"");
[/code


Pete
clark238

Re: Trim() not working SOLUTION!!!

Post by clark238 »

Wow, I'm bald at this point, but I figured it out...

You just need to Trim() and then reset the canvas size:

Code: Select all

$image->Trim();
$image->Set(page=>'0x0+0+0');
[code]

HUGE RELIEF!  :D 

Thanks to everyone who replied. Double thanks to El_supremo who got me googling for "page"!
Post Reply