ash
ash

Reputation: 107

How to append a same image to multiple images using imagemagick

Hello I want to add a same image to the left side of multiple images. First image is a legend and it common for all the 6 images which I later want to montage 3x2.

I tried this command below before montaging and it did not work. I wanted to see if I could make it work without adding a for loop, which slows down the code.

convert +append image_3_1.png image_1_[1-6].png -geometry +10+0 test.png

I want the image_3_1 added to all the 6 images starting with image_1. Any ideas?

Upvotes: 2

Views: 820

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208077

Your question is unclear about:

  • what your input and expected output images look like or how big they are,
  • whether you actually need the intermediate images or just the montage,
  • what the actual issue is with for loops

So here are some ideas...


Option 1

This one avoids for loops and multiple invocations of magick by using a single magick and loading the side image just once and cloning it in memory:

magick side.png \
   \( +clone image_1.png +append -write out_1.png +delete \) \
   \( +clone image_2.png +append -write out_2.png +delete \) \
   \( +clone image_3.png +append -write out_3.png +delete \) \
   \( +clone image_4.png +append -write out_4.png +delete \) \
   \( +clone image_5.png +append -write out_5.png +delete \) \
   image_6.png +append out_6.png 

It produces 6 output files as follows:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here


Option 2

This one avoids for loops by running 6 copies of magick in parallel:

magick side.png image_1.png +append out_1.png &
magick side.png image_2.png +append out_2.png &
magick side.png image_3.png +append out_3.png &
magick side.png image_4.png +append out_4.png &
magick side.png image_5.png +append out_5.png &
magick side.png image_6.png +append out_6.png &
wait

It produces the same 6 output files as above.


Option 3

This does the same by using GNU Parallel to do it more succinctly:

parallel magick side.png image_{}.png +append out_{}.png ::: {1..6}

Option 4

If you don't need the intermediate files, and just want the montage:

parallel -k magick side.png {} +append ppm:-  ::: image_*png | magick montage -tile 2x3 -geometry +5+5 ppm:- montage.png

enter image description here


Option 5

This is much the same, avoiding producing the intermediate output files, and also avoiding using GNU Parallel:

magick side.png \
   \( +clone image_1.png +append -write ppm:- +delete \) \
   \( +clone image_2.png +append -write ppm:- +delete \) \
   \( +clone image_3.png +append -write ppm:- +delete \) \
   \( +clone image_4.png +append -write ppm:- +delete \) \
   \( +clone image_5.png +append -write ppm:- +delete \) \
   image_6.png +append ppm:- | magick montage -background black -geometry +5+10 -tile 2x3 ppm:- montage.png

enter image description here


Option 6

This one uses no for loops, a single process, no separate montage command and generates no intermediate files:

magick side.png -write MPR:side +delete \
   \( MPR:side image_1.png  MPR:side image_2.png +append \) \
   \( MPR:side image_3.png  MPR:side image_4.png +append \) \
   \( MPR:side image_5.png  MPR:side image_6.png +append \) \
   -append montage.png

enter image description here

Replace the +append and -append with -smush for more layout and inter-image spacing flexibility.


Option 7

Maybe something like this with -smush:

magick side.png -write MPR:side +delete -background cyan \
   \( MPR:side image_1.png  MPR:side image_2.png +smush 10 \) \
   \( MPR:side image_3.png  MPR:side image_4.png +smush 10 \) \
   \( MPR:side image_5.png  MPR:side image_6.png +smush 10 \) \
   -smush 30 montage.png

enter image description here


My guess is that option 6 would be the fastest on most machines in most circumstances, if it is flexible enough for you. If you need more flexibility, go with option 7 or 5.

Keywords: ImageMagick, image processing, montage, layout, parallel, smush.

Upvotes: 2

Related Questions