How to combine pairs of images to single images?

What kind of format are these images?

If they are saved in a lossy compression format (e.g. JPEG), you will either have more or less quality loss or end up with very large files. This is due to the fact, that you will have to apply a lossy compression step a second time to keep the file size reasonable. If you can live with a little quality loss and /or some increase in file size you could for example recompress the images into a new JPEG. This can be done in the shell and options for compression quality can be set.

If you have a lossless format (e.g. TIFF), there will be no problem to reencode the images.

They are in jpeg - after converting using rawtherapee at the highest quality

should I save in TIFF and then combine or is there another one? saving in TIFF will take me a whole day since each image is differently cropped

If the final combined image is 5-20% smaller than the total size of the pair, then I can work with that

So you want the final image as JPEG directly side by side or top and bottom? Can you post an example?

since each image is differently cropped

Do all images have a comon dimension (top or side)? Otherwise combination could be more difficult.

If the final combined image is 5-20% smaller than the total size of the pair, then I can work with that

If your final image is smaller than the total size of the pair, than the quality will be lower. Combination is not a way to save space.

I’ve found a way to keep the quality at it’s highest but there’s still a black gap in the middle

The code I’ve used is

montage *.jpg -tile 0x -quality 100 -background none -geometry +2+2 result.jpg

The height of all the images is the same: 1080 px

While the width changes from the lowest being 480 px to the highest being 1918px

The code is what you make it.

http://www.imagemagick.org/Usage/montage/

@Chintan_Gohel I think you should use “-geometry +0+0” if you don’t want a gap …

convert -quality 50 Image_L.jpg Image_R.jpg +append Result.jpg

Gives this:

Are all images mirrored? Than it could be done easier :grinning:.

I found that out on my own, and I’ve got a working code that uses adjoin instead of tile

Now I’m trying to get it to work in a “for” statement

Question - I’m almost there but I can’t seem to get the for statement working as I want it to

This is what I had written

for index in $(seq 1 2 10)
do
montage -adjoin -quality 100 -background none -geometry +0+0 $index.jpg
done

Now what happens is that if there are more than 2 images, the combined image keeps on combining the other pairs so that it has 4 images combined, and then 8 and then 12 and keeps on getting bigger and bigger

@Chintan_Gohel The best place to ask your ImageMagick questions is on its forum: http://www.imagemagick.org/discourse-server/viewforum.php?f=1

Like @Thomas_Do, I would use +append. Also, height won’t matter if you resized the images. Try doing

magick rose: logo: -resize x400 +append result.png

As for the for statement, what you are currently doing is repeating the montage action and adding your output file in every next iteration. This will gobble up your memory and disk space fast. Instead, the image pairs should be replacing rose: and logo: in my example and the result filename should also be changing. My bash is a little rusty so I will let someone else help you with that.

PS magick is IM7’s convert, so if you are using IM6, do

convert rose: logo: -resize x400 +append result.png

I’ve noticed that the bash console would hang and now 6Gb have been used up - how can I clear that?

You missed my new post just now :slight_smile:. Please read it.

If your files are in alphabetical order and you want then to join successive pairs, this should work:

array=($(find . -name "*.jpg"| sort)); for (( i=0; i<${#array[@]}/2; i++ )); do convert -quality 100 ${array[i*2]} ${array[i*2+1]} +append Result$i.jpg; done

However, the files are not allowed to contain whitespaces.

Edit: changed to formatted code with backticks

  1. Please see Format your code to make it more readable
  2. The way you used $index makes montage only use it as a target, meaning you did not specify the source files, so montage uses all the files it finds as source and explodes when you run out of RAM. Instead, if you refer to the ridiculously long documentation I linked to in my previous post, you should specify the source files first, and the target file last. So change:
for index in $(seq 1 2 10); do
    montage -adjoin -quality 100 -background none -geometry +0+0 $index.jpg
done

to

for i in {1..100..2}; do
    montage -quality 92 -background white -tile 2x1 -geometry +0+0 "${i}.jpg" "$((i+1)).jpg" "output_${i}-$((i+1)).jpg"
done

To help you understand the code:

1 Like

I read it but I haven’t tried the rose logo code yet
And yes, a lot of space has been used up and I also have to worry about getting that space back

I tried this but nothing seems to happen - I don’t see any output or new files

I’m getting a bad substitution error

There was a formatting error, you have to format code here as preformatted text. I edited the post. Please try again. You have first to “cd” into the right directory.

Fixed, I changed ${001..100..2} to {1..100..2}