Combining multiple images in ImageMagick with relative (not absolute) offsets

Am I looking for the most efficient way to stitch multiple images together in ImageMagick on top of a background image so that the spacing / indent between overlays is consistent?

I explored the use of + append, convert -composite and convert by merging -page and -layers.

The following command (convert-compposite) works, but requires a preliminary image size calculation to indicate absolute offsets. Indeed, I want a 10-pixel gap between the end of the FIRST layer image and the beginning of the second layered image, but the only way I can see to achieve this is to specify the absolute offset from the upper left corner of the canvas.

convert \ background.jpg \ first.jpg -gravity Northwest -geometry +10+10 -composite \ second.jpg -geometry +300+10 -composite \ third.jpg -geometry +590+10 -composite \ output.jpg 

I'm looking for a kind of operator, so that the horizontal offset can be interpreted relative to the "last" image in the layer, so instead of specifying +300+10 for the second image and +590+10 for the third, I can somehow specify the offset +10+10 .

I thought gravity would allow me to achieve this ( -gravity Northwest ), in the same way as float: left; works in CSS positioning, but it is not.

I also had some success with the following:

 convert \ -page +10+10 first.jpg \ -page +300+10 second.jpg \ -page +590+10 third.jpg \ -background transparent \ -layers merge \ layered.png convert background.jpg layered.png -gravity Center -composite output.jpg 

Both methods described above require a preliminary calculation of the absolute displacements, which is a little painful. Is there a better way to do this?

+7
source share
2 answers

You missed the montage command.

The simplest command to add the desired interval with it would be to set the -frame 5 option with -mattecolor none . This works with images of different widths and spaces, all located at a distance of 10 pixels:

 montage \ -alpha on \ -background none \ -mode concatenate \ -tile x1 \ -frame 5 \ -mattecolor none \ *.jpg \ output1.png 

You will easily notice that the resulting border of the image is only 5 pixels wide at the top, right, bottom and left. To remove these 5 pixels around, use:

 convert output1.png -shave 5 output2.png 

To overlay this result on your background.jpg , use:

 convert \ background.jpg \ output2.png \ -gravity Northwest \ -geometry +10+10 \ -composite \ final.jpg 
+7
source

You can also use the transparent Kurt trick with the addition. Using append instead of editing has the advantage that you can use gravity settings to align your top (north) north (north) or center.

Here is an example of how to add images horizontally with a 10-pixel gap between them and with aligned images:

 convert \ -frame 5 \ -mattecolor none \ -background none \ -gravity north \ first.jpg second.jpg third.jpg \ +append \ png:- | convert - -shave 5 output.png 

To add images vertically, use -append instead of + append. I used the handset | to shave the outer frame in the same command.

+2
source

All Articles