Page 1 of 1
Gravity weighs heavy on newbie
Posted: 2007-02-07T06:54:10-07:00
by asarian
Dear people,
Using Image::Magick -- 6.3.2-0 (latest FreeBSD port from CVS), I have a question about 'gravity'. Consider please the following snippet of Perl code:
$image->Set (quality => 100);
$image->Scale (geometry => '150x150');
$image->Extent (geometry => '150x150', gravity => 'Center', ...);
$image->Write ($thumbnail);
Can't get the gravity orientation to work, though. I always get a thumbnail, starting at 0x0, which is extended southward (if it's, say, 150x84), whereas I expected the image to become centered on the (black) 150x150 canvas.
A rather helpful person on the ImageMagick mailing list suggested I ask in this forum. He mentioned that 'gravity' may have only been added after 6.3.2-0. I get no error messages of any kind; gravity just seems to be plainly ignored.
Anthony, the helpful individual, also suggested the Perl API may be behind on the Core stuff. Can someone shed some ligjt on this please?
Thanks,
- Mark
Re: Gravity weighs heavy on newbie
Posted: 2015-03-19T14:36:23-07:00
by vitz
Hi,
I have the same problem described above but with last versions of IM (e.g. ImageMagick-6.9.0-10-Q16-x64-dll.exe, ImageMagick-6.8.7-5-Q16-x64-dll.exe). It seems that gravity option is ignored and is used northwest all the time.
Please, see this sample code:
------------------------------------------
#!/usr/bin/perl
use Image::Magick;
$image = Image::Magick->new;
$x = $image->Read('src.png');
$image->Extent( width=>$image->Get('width')+20, gravity=>'East');
$image->Extent( width=>$image->Get('width')+20, gravity=>'West');
$image->Extent( height=>$image->Get('height')+20, gravity=>'Top');
$image->Extent( height=>$image->Get('height')+20, gravity=>'Botton');
$image->Write('src-out.png');
------------------------------------------
I expected that 'src-out.png' should have 20 px border all around. But I obtained 40 px on rigth side and 40 px on bottom.
I am sure it worked well in some several years old version of IM.
I am not sure if this is a right forum for such bug report, but I did not find fetter for PerlMagick.
Thanks
Vit
Re: Gravity weighs heavy on newbie
Posted: 2015-03-19T16:36:01-07:00
by snibgo
I don't know about Perl, but at the command line "top" and "bottom" are not valid values for gravity. We would use "north and "south".
Re: Gravity weighs heavy on newbie
Posted: 2015-03-24T15:00:24-07:00
by vitz
Yes you are right, Snibgo. It was my mistake only when I wrote the example. Sorry. There is corrected example illustrating wrong behaviour:
--------------------------------------
use Image::Magick;
$image = Image::Magick->new;
$x = $image->Read('src.png');
$image->Extent( width=>$image->Get('width')+20, gravity=>'East');
$image->Extent( width=>$image->Get('width')+20, gravity=>'West');
$image->Extent( height=>$image->Get('height')+20, gravity=>'North');
$image->Extent( height=>$image->Get('height')+20, gravity=>'South');
$image->Write('src-out.png');
--------------------------------------
Commandline ImageMagick programs do analogue work well.
It seems like bug in PerlMagick or GUI breaking change that I did not notice anywhere.
Thanks Vit
Re: Gravity weighs heavy on newbie
Posted: 2015-03-24T17:01:17-07:00
by magick
You must use the geometry argument for relative positioning while respecting gravity. Width / height / x / y are absolute locations and do not respect gravity. To fix, create a geometry string from $image->Get('width')+20, and use this method, $image->Extent(geometry=>$geometry,gravity=>'East'). Alternatively, compute the absolute (x,y) position yourself relative to the gravity. Since Perl is a scripting language, more of the burden falls on the developer as compared to the ImageMagick command line.
Re: Gravity weighs heavy on newbie
Posted: 2015-03-29T01:58:01-07:00
by vitz
I see Magick! Thank you very much. There are three equivalent solutions:
-----------------------------------------------------
use Image::Magick;
$leftBorder = 10;
$rightBorder = 20;
$topBorder = 30;
$bottomBorder = 40;
$image = Image::Magick->new;
$x = $image->Read('src.png');
# Solution 1
#$geometry = ($image->Get('width')+$leftBorder) . 'x' . ($image->Get('height'));
#$image->Extent( geometry=>$geometry, gravity=>'East');
#$geometry = ($image->Get('width')) . 'x' . ($image->Get('height')+$topBorder);
#$image->Extent( geometry=>$geometry, gravity=>'South');
#$geometry = ($image->Get('width')+$rightBorder) . 'x' . ($image->Get('height'));
#$image->Extent( geometry=>$geometry, gravity=>'West');
#$geometry = ($image->Get('width')) . 'x' . ($image->Get('height')+$bottomBorder);
#$image->Extent( geometry=>$geometry, gravity=>'North');
# Solution 2
#$geometry = ($image->Get('width')+$leftBorder) . 'x' . ($image->Get('height')+$topBorder);
#$image->Extent( geometry=>$geometry, gravity=>'SouthEast');
#$geometry = ($image->Get('width')+$rightBorder) . 'x' . ($image->Get('height')+$bottomBorder);
#$image->Extent( geometry=>$geometry, gravity=>'NorthWest');
# Solution 3
$geometry = ($image->Get('width')+$leftBorder+$rightBorder) . 'x' . ($image->Get('height')+$topBorder+$bottomBorder)
. '-' . $leftBorder . '-' . $topBorder;
$image->Extent(geometry=>$geometry);
$image->Write('src-out.png');
---------------------------------------------------------
Vit