Page 1 of 1

Center layers relative to each other

Posted: 2016-07-03T12:45:49-07:00
by sas
Consider a folder with a bunch of images with different dimensions. For testing purposes, we can use these input images:

Code: Select all

mkdir frames
convert rose: frames/01.png
convert granite: frames/02.png
Now I'd like to combine that whole folder into a single GIF animation.

The following works, but it aligns the images to the top left corner:

Code: Select all

convert -delay 50 frames/*.png -layers trim-bounds out.gif
Image

How can I automatically center all frames (both vertically and horizontally), without having to manually specify a -page offset for each one?

Re: Center layers relative to each other

Posted: 2016-07-03T13:26:48-07:00
by GeeMack
sas wrote:How can I automatically center all frames (both vertically and horizontally), without having to manually specify a -page offset for each one?
This can be done without much difficulty if you're using ImageMagick 7. Using "ImageMagick 7.0.2-2 Q16 x64" on Windows 7 64, I would use something like this command...

Code: Select all

magick -delay 50 frames/*.png ^
   ( -clone 0--1 -layers merge -set option:max_w %[w] -set option:max_h %[h] +delete ) ^
   -gravity center -background none -extent %[max_w]x%[max_h] -layers trim-bounds out.gif
First that calls in all your images from the "frames" folder.

Next it clones them all and makes them into a single merged layer containing all the images full width and height.

Then it sets a couple of variables, one for the width of that merged image which will be the size of the widest image in the folder. The second variable is for the height and will be the size of the highest image in the folder.

After it gets those dimensions into variables it deletes that temporary merged image.

Next it sets the "-gravity" setting to "center", and it sets the "-background" to "none".

Now the "-extent" operator uses those two variables we made previously to place each image in the center of a transparent background canvas, the total size being the maximum width and height of the largest source images.

Finish by running your "-layers trim-bounds" operation, although at that point I don't think you actually need it.

Give it the output file name, and all the source images are centered and stacked into a GIF animation.

Re: Center layers relative to each other

Posted: 2016-07-03T14:05:30-07:00
by fmw42
To the OP:

Please, always provide your IM version and platform when asking questions, since syntax may differ.

See viewtopic.php?f=1&t=9620