New page with gravity

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
zipicip
Posts: 6
Joined: 2011-07-29T01:18:10-07:00
Authentication code: 8675308

New page with gravity

Post by zipicip »

Hello.

I have a script that compose 2 images for an animation

convert -dispose previous -delay 200 \
-page 300x169+0+0 1_tmp.png \
-page 300x169+0+0 2_tmp.png \
-loop 0 animation.miff


The images are smaller than the 300x169 new canvas and I want that the two images to be centered in the new canvas


convert -dispose previous -delay 200 \
-page 300x169+0+0 -gravity center 1_tmp.png \
-page 300x169+0+0 -gravity center 2_tmp.png \
-loop 0 animation.miff


This doesn't work.

Another idea if I cannot use gravity is to try to get for each image current width and put margin

Ex. ... if the image width = 200, canvas_width = 300 , I get -page 300x169+50+0 .... but I don't know how to rewrite the line so I can get half of the current image width
Last edited by zipicip on 2011-07-29T05:06:23-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: New page with geometry

Post by Bonzo »

Try -gravity:

Code: Select all

convert -dispose previous -delay 200 \
 -page 300x169+0+0 -gravity center 1_tmp.png \
 -page 300x169+0+0 -gravity center 2_tmp.png \
 -loop 0 animation.miff
zipicip
Posts: 6
Joined: 2011-07-29T01:18:10-07:00
Authentication code: 8675308

Re: New page with geometry

Post by zipicip »

my mistake from the beginning I was meaning gravity .... so already tried
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: New page with gravity

Post by fmw42 »

-page is not to my knowledge gravity sensitive. It is something that I have been asking for in IM 7 or something like that under the appropriate conditions.

see comment at http://www.imagemagick.org/Usage/layers/#flatten

Use convert ... -gravity ... -geometry ... -compose ... -composite

see http://www.imagemagick.org/Usage/layers/#convert
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: New page with gravity

Post by anthony »

-page which is defining virtual canvases does NOT make use of gravity! It is not part of the definition of virtual canvases (from GIF animation), or image layering (from GIMP and photoshop). The meaning of virtual canvas and the offset of the image is very clear and well defined. (virtual canvas is positioning canvas size relatitve to the origin 0,0, while the offset is simply a positive direction offset from that fixed 0,0 origin to the top level corner of the image.


However using -geometry to define composition offsets does allow the use of gravity to define the meaning of the offset, as a spacing between an image and another image (edges, corners, or middles) that is typically larger image (though not always).

What is currently NOT defined (and has been requested MANY times) is a justification of the image relative to the specified gravity defined offset (defaulting to same direction as the gravity definition, unless changed). This is one of the features that I want added to IMv7!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zipicip
Posts: 6
Joined: 2011-07-29T01:18:10-07:00
Authentication code: 8675308

Re: New page with gravity

Post by zipicip »

ok... Thanks a lot.

I have another question:

I use php to execute exec commands like:

exec ("convert -size 300x250 gradient:" . $gradient_up . "-" . $gradient_down . " canvas_300x250.png");

The command above works fine.

The problem:

I want to compose in a php string multiple imagick commands so when a have all commands I need in one string to execute at the end of the program

exec (all_imagick_comands);

But this doesn't execute right

For example:

$frame = '';
foreach ($array as $image) {
$frame .= " -page 300x250+0+0 imagine_name_" . $image . "_300x250.png \ \n";
}

exec (" convert -dispose previous -delay 200 \
" .$frame . "
-loop 0 animation.miff ");

I am missing something ?
zipicip
Posts: 6
Joined: 2011-07-29T01:18:10-07:00
Authentication code: 8675308

Re: New page with gravity

Post by zipicip »

I managed to do this

the mistake: I was adding a space after slash or new line before command end.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: New page with gravity

Post by anthony »

IM convert can do all the convert commands in one command. Even if it means deleteing the image you have finished and written out, and starting with a new image. Nothing stops you from doing this.

the problem is if you need to do some shell programming between steps.

However PHP exec() string is actually just a shell script. You can have mulpile commands or even shell if-then or loops in it. Just follow normal shell scripting, but adding newlines or ';' between the individual commands.

Eg:

Code: Select all

  exec("command args; command args; command args");
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zipicip
Posts: 6
Joined: 2011-07-29T01:18:10-07:00
Authentication code: 8675308

Re: New page with gravity

Post by zipicip »

another question regarding resizing an image.

I have an area let say: 145x115

Some images are portrait and some are landscape.

So I want an image size to fit this area.

The problem:

If I use

$this->max_width_image = '145';
$this->max_height_image = '115';

convert image.png -resize " . $this->max_width_image . "x\> image_tmp.png
convert image.png -resize x". $this->max_height_image . "\> image_tmp.png

This will resize to smallest height. This is not good. I just only to resize to width or height only if necessary.

Let say after first resize image has 145x200 . Only then I want to lose from width so can fit area. The problem here if initial width is smaller than height the image will be resized to width first.

So I need to know first the image width and height. But I don't know how to use that with exec from php.
zipicip
Posts: 6
Joined: 2011-07-29T01:18:10-07:00
Authentication code: 8675308

Re: New page with gravity

Post by zipicip »

I guess I managed myself

img_width=identify -format '%w' image.png
img_height=identify -format '%h' image.png


if [ {img_width} -eq {img_height} ]
then
convert image.png -resize x". $this->max_height_imagine . "\> image.png
elif [ {img_width} -gt {img_height} ]
then
convert image.png -resize x". $this->max_width_imagine . "\> image.png
convert image.png -resize x". $this->max_height_imagine . "\> image.png
else
convert image.png -resize x". $this->max_height_imagine . "\> image.png
convert image.png -resize x". $this->max_width_imagine . "\> image.png
fi
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: New page with gravity

Post by anthony »

zipicip wrote:I have an area let say: 145x115
Some images are portrait and some are landscape.
So I want an image size to fit this area..
That is automatic defaults for resize! Just resize the image to 145x115 and the image will fit into that area!
No need to do the tests yourself, just do it!

See Resize examples...
http://www.imagemagick.org/Usage/resize/#resize
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply