Search code examples
imagemagickcentering

imagemagick nested gravity centering


I am trying to composite two images with gravity, and then position them within a larger image at a geometry.

When I try

magick  -size 1045x837 xc:blue   \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 -gravity center \) -composite \) -geometry +26+53 -composite test-y.png

I get

and when I do

magick  -size 1045x837 xc:blue   \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437  \) -composite  \) -geometry +26+53 -composite test-x.png

I get

I think this involves clone and related, maybe similar to this answer, but I just can't find the combo.

What do I need to do, to get the white centered within the red, and geometrically placed in the upper-left corner?


Solution

  • It's not clear exactly what you want, but I think you are falling foul of the fact that -gravity is a "setting". As such, it remains set until changed, so you probably want this, where I reset the gravity to NorthWest before the final composite:

    magick  -size 1045x837 xc:blue   \( -size 345x437 xc:red \( -size 275x417 xc:white -resize 345x437 -gravity center \) -composite \) -gravity northwest -geometry +26+53 -composite  result.png 
    

    enter image description here

    You might find -extent a simpler way to fill out the white to a given size using a red background:

    magick  -size 1045x837 xc:blue \( -size 275x417 xc:white -resize 345x437 -background red -gravity center -extent 345x437 \) -gravity northwest -geometry +26+53 -composite  result.png