Center layers relative to each other

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
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Center layers relative to each other

Post 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?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Center layers relative to each other

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Center layers relative to each other

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