Gravity weighs heavy on newbie

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

Gravity weighs heavy on newbie

Post 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
vitz
Posts: 3
Joined: 2015-03-15T10:27:03-07:00
Authentication code: 6789

Re: Gravity weighs heavy on newbie

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gravity weighs heavy on newbie

Post 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".
snibgo's IM pages: im.snibgo.com
vitz
Posts: 3
Joined: 2015-03-15T10:27:03-07:00
Authentication code: 6789

Re: Gravity weighs heavy on newbie

Post 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
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Gravity weighs heavy on newbie

Post 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.
vitz
Posts: 3
Joined: 2015-03-15T10:27:03-07:00
Authentication code: 6789

Re: Gravity weighs heavy on newbie

Post 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
Post Reply