imagemagickimagemagick-convert

flatten layers aligned by centers instead of aligned by top-left corners


How can I overlap several images so that their centers are directly on top of each others'?

I use -layers merge instead of -layers flatten because flatten will crop images to fit inside the dimensions of the first image, but merge expands the "canvas" instead, and I want to be able to see the entirety of each of the input images.

The default behaviour of -layers merge and -layers flatten is to align the top-left corners:

convert wizard: \( logo: -transparent white \) \( rose: -transparent white \) -background white -layers merge merged.png

enter image description here

You can see the rose is in the top-left, but I would like it to be in the center. I would also like the wizard: image to be center-aligned.

Desired output:

enter image description here


Solution

  • ===== Edited for TL;DR =====

    Having tried several possible solutions to stack multiple images, including the full dimensions of all the images, and using their centers as the alignment point, I've landed on this simple command which will work with ImageMagick versions 6 or 7.

    magick -background white ^
       wizard: ( logo: -transparent white ) ( rose: -transparent white ) ^
       -set page -%[fx:s.w/2]-%[fx:s.h/2] -layers merge result.png
    

    Below is my original reply and edits. Much of it more complicated than need be.

    ====================

    One ImageMagick v6 solution to centering and merging multiple images of different dimensions is to set the paging geometry of each image before merging. (A more straightforward solutions using IMv7 is included below.) Here is an example in Windows CLI syntax that uses IM's FX expressions to calculate the dimensions and centers...

    convert ^
       wizard: ( logo: -transparent white ) ( rose: -transparent white ) ^
       ( -clone 0--1 -layers merge ) ^
       -set page %[fx:u[-1].w]x%[fx:u[-1].h] ^
       -set page +%[fx:u[-1].w/2-(s.w/2)]+%[fx:u[-1].h/2-(s.h/2)] ^
       +delete -background white -layers merge result.png
    

    First that reads in the images and does the necessary transparencies.

    Then in parentheses it clones all the input images and merges them to make a template we'll use to get the finished page size.

    Next it sets the paging dimensions on all the images, in this case the width by height of that template image.

    Then for each image it sets the page geometry by calculating half the width of the template image minus half the width of the input image. And same thing for setting the height geometry.

    After the dimensions and geometry locations are set, the template image is removed with "+delete", the background is set to white, and the layers are merged using the specially set geometry.

    EDITED AGAIN: For ImageMagick v6 the use of FX expressions is limited to certain kinds of settings. With IMv7 the FX expressions can be used almost anywhere.

    This IMv7 command just resets the paging geometry so point "0,0" is actually outside each image by half its dimensions instead of at the top left. Note the page setting are negative numbers -%[fx:s.w/2]. Then "layers merge" uses that geometry to align the images properly and flatten them.

    magick -background white ^
       wizard: ( logo: -transparent white ) ( rose: -transparent white ) ^
       -set page -%[fx:s.w/2]-%[fx:s.h/2] -layers merge v7result.png
    

    Here is another command in IMv7 syntax. It calculates the final dimensions by merging the images inside parentheses, and using those dimensions with "-gravity center" to extend each image's canvas to the dimensions of the template image.The template is deleted and the rest are flattened onto a white background.

    magick -background none -gravity center ^
       wizard: ( logo: -transparent white ) ( rose: -transparent white ) ^
       ( -clone 0--1 -layers merge ) -extent  %[fx:u[-1].w]x%[fx:u[-1].h] ^
       +delete -background white -flatten v7result.png
    

    This is the result of any of the above commands...

    enter image description here