AutoMerge two images files to one

670 Views Asked by At

I am using Montage command-line tool to merge the two jpg images. The output jpg contains the common strip present in the input jpgs. Below is the command to merge two jpgs:

montage -geometry 500 input1.jpg input2.jpg output.jpg

How can I avoid the common area in the output file? Is there any other tool available to auto-merge the two images?

4

There are 4 best solutions below

0
On

In ImageMagick, you can simply append the two images side by side or top/bottom.

convert image1.jpg image2.jpg -append result.jpg

will do top/bottom

convert image1.jpg image2.jpg +append result.jpg

will do left/right.

You can append as many images as you want of different sizes. You can use the -gravity setting to align them as desired. If different sizes, then you will have background regions, which you can control the color by using -background somecolor. If desired, you can resize the images by adding -resize 500 after reading the inputs and before the append.

See http://www.imagemagick.org/Usage/layers/#append

1
On

If you want to do what Mark Setchell is suggesting, then using -page is probably the best method, if you have more than one image to merge and the offsets are different. If you only have on pair of image, you can overlap them using +smush in ImageMagick. It is like +append, but allows either overlap or a gap according to the sign of the argument. Unlike -page, it only shifts in one direction according to +/- smush. Using Mark's images,

convert left.jpg right.jpg +smush -400 result.jpg

enter image description here

0
On

I suspect you are trying to make a panoramic by stitching two images with an area of common overlap.

So, if we start with left.png:

enter image description here

and right.png:

enter image description here

You probably want this:

convert left.png -page +200 right.png -mosaic result.png

enter image description here


Just so you can see what happens if I change the x-offset and also how to add a y-offset:

convert left.png -page +280+30 right.png -mosaic result.png

enter image description here

0
On

Before finding this answer I ended up with a slightly different strategy:

  • (specific to me) -resize images if they have different scale (dpi)
  • Position each image in a canvas of the desired merged dimensions (left+right-overlap):
    • -extent are the final dimensions
    • -gravity positions each image on the top/bottom left/right
    • -background none so the empty part aren't white
  • pile them up with -composite

(ImageMagick 7 code, just replace magick with convert for IM6)

sz=1105x1155
magick \
  \( \( right.jpg -resize 924x724^ \) \
              -background none -gravity northeast -extent $sz \) \
  \( left.jpg -background none -gravity southwest -extent $sz \) \
-composite result.jpg

This seems less elegant than the existing answers, for example -mosaic used by @Mark Setchell is smarter, according to the docs:

Rather than only creating an initial canvas based on just the canvas size of the initial image, the Mosaic Operator creates a canvas that is large enough to hold all the images (in the positive direction only).

... but maybe some use-case will prefer giving the merged canvas size or need -composite options.