imagemagick montage: insert images of different sizes

762 Views Asked by At

I have an image a.png of size 800x600, and two images b.png and c.png of size 400x300 each. I want to montage (insert) the three images without size changes into a png image of size 800x900, such that a.png is at the top, and b.png and c.png side-by-side underneath.

How can I do this with imagemagick convert or montage?

1

There are 1 best solutions below

1
Mark Setchell On BEST ANSWER

Generate sample images, 1/10th the real size:

magick -size 80x60 -background red  -gravity center label:"A"  a.png
magick -size 40x30 -background lime -gravity center label:"B"  b.png
magick -size 40x30 -background blue -gravity center label:"C"  c.png

Now, you can load A, and inside parentheses, load B and C and place side-by-side as a new image, then append that result below A:

magick a.png \( b.png c.png +append \) -append result.png

enter image description here

Or, if you dislike the parentheses, you can join B and C side-by-side into a single image, load A, swap the order so A is at the top and then append the combined image below:

magick b.png c.png +append a.png +swap -append result.png

Note the distinction between:

  • +append meaning "append to the right", and
  • -append meaning "append below".

Note that, in addition to +append and -append, there are the newer tools called +smush and -smush which do exactly the same but take a parameter which is the number of pixels to offset the appending. So if you do +smush 5 it will do the same as +append but leave 5 pixels of background colour showing in the new gap between the images. If you use a negative offset, it will append in the same place but overlap the two images by the offset.

Here's an example, I make the background magenta and smush B and C with a 10 pixel gap. Then change the background to yellow before smushing the result below A with a 15 pixel offset.

magick -background magenta  b.png c.png +smush 10  a.png +swap -background yellow -smush 15  result.png

enter image description here

If still using v6 ImageMagick, replace magick with convert.