150

I have five images of sizes: 600x30, 600x30, 600x30, 600x30, 810x30. Their names are: 0.png, 1.png, 2.png, 3.png, 4.png, respectively.

How do I merge them Horizontally to make an image of size 3210x30 with ImageMagick?

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Sasuke Kun
  • 2,449
  • 3
  • 15
  • 14

6 Answers6

193

ImageMagick ships with the montage utility. Montage will append each image side-by-side allowing you to adjust spacing between each image (-geometry), and the general layout (-tile).

montage [0-4].png -tile 5x1 -geometry +0+0 out.png

Other examples can be found on Montage Usage page

luator
  • 4,769
  • 3
  • 30
  • 51
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • 1
    montage documentation specifies that the [0-5] syntax is for linux, so I guess if you are on windows you have to type/generate all the file names. i.e. montage 0.png 1.png 2.png 3.png 4.png ... – Dan Jul 26 '14 at 01:59
  • @RodrigoGraça See [Usage & Examples](http://www.imagemagick.org/Usage/montage/) guide about handling transparency + backgrounds. – emcconville Aug 05 '16 at 17:22
  • it works for combination of jpg and png as well. thanks – joydeep bhattacharjee Feb 13 '18 at 12:22
  • 6
    `[0-5].png` will produce "0.png, 1.png, 2.png, 3.png, 4.png, 5.png", six different files, but your `tile 5x1` will only take five of them, leaving you with two output files. This should either use `[0-4].png` or `-tile 6x1`. – R. Barrett Aug 28 '19 at 14:36
  • Thanks for the bracket tip! Been using linux for years and only found `{0..4}`. See the "Ranges" section of the [glob manual](https://man7.org/linux/man-pages/man7/glob.7.html) for documentation. – young_souvlaki Sep 10 '21 at 18:31
  • 1
    Following your lead, this worked perfectly to combine 2 jpegs side-by-side!: `montage left.jpg right.jpg -tile 2x1 -geometry +0+0 out.jpg` – Gabriel Staples Mar 24 '22 at 06:49
  • You might want to add `-background none` to preserve transparency. – Liran Funaro Sep 25 '22 at 09:13
129

ImageMagick has command line tool named 'convert' to merge images horizontally, or for other purpose. i have tried this command and working perfectly on your case:
To join images horizontally:
convert +append *.png out.png

To stack images vertically:
convert -append *.png out.png

higginse
  • 312
  • 2
  • 5
tesmojones
  • 2,496
  • 2
  • 21
  • 40
  • 1
    that can be added to a Thunar custom action in this form (ex. to join vertically = up-town):`convert -append %F joined-image.png` . All selected images will be joined as one png in alphabetical order –  Mar 05 '17 at 10:53
  • 3
    If using ImageMagick 7+ you'll need to put the images you're working on before the parameters: `convert *.png +append out.png` – Mazuhl Feb 05 '18 at 15:01
  • 1
    To make a grid 2x2 (a.png, b.png, c.png, d.png): `convert a.png b.png +append row1.png; convert c.png d.png +append row2.png; convert row1.png row2.png -append grid.png` – deadfish Feb 10 '21 at 10:02
23

Use -resize if the images don't have the same width/height

You can fix the height for all of them with the -resize option, e.g. to fix a 500 pixel height on two images joined horizontally:

convert +append image_1.png image_2.png -resize x500 new_image_conbined.png

Or for vertical joins, you would want to set a fixed width instead:

convert -append image_1.png image_2.png -resize 500x new_image_conbined.png

Example:

image_1.png 1067x600

enter image description here

image_2.png 1920x1080

enter image description here

new_image_conbined.png 889x500

enter image description here

Related:

How to do it interactively with GIMP

If you need to crop/resize images interactively first, which is often the case, then GIMP is the perfect tool for it, here's a detailed step-by-step: https://graphicdesign.stackexchange.com/questions/83446/gimp-how-to-combine-two-images-side-by-side/145543#145543

enter image description here

SVGs

ImageMagick 6.9.11-60 doesn't handle them, so see:

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
12

Very simple with ImageMagick (brew install imagemagick )

convert +append image_1.png image_2.png new_image_conbined.png
HaloWebMaster
  • 905
  • 6
  • 16
Thiago
  • 12,778
  • 14
  • 93
  • 110
  • 4
    Nice... Copy an existing answer, get +10 votes. – Quasímodo May 24 '21 at 13:07
  • When I run this, for some reason, one of my images end up being rotated. So for example, imagine I'm trying to stack V on top of E. Instead of getting: V E I get something that looks like: ^ E (I'm using the '-append' option) – Raleigh L. Oct 05 '22 at 07:06
1

Anyone using the MiniMagick rails gem can use the built-in tool to merge images:

# Replace this with the path to the images you want to combine
images = [
  "image1.jpg",
  "image2.jpg"
]

processed_image = MiniMagick::Tool::Montage.new do |image|
  image.geometry "x700+0+0"
  image.tile "#{images.size}x1"
  images.each {|i| image << i}
  image << "output.jpg"
end

Check out the documentation for #geometry options to handle resizing and placement. The current example will resize images to a 700px height while maintaining the image's aspect ratio. +0+0 will place the image with no gaps between them.

Myk Klemme
  • 540
  • 1
  • 4
  • 15
1

The convert +append method described in other answers appends images horizontally, aligned to the top. If you prefer to align to the bottom or center, try:

convert input1.png input2.png -gravity South +append output.png
  or
convert input1.png input2.png -gravity Center +append output.png

Source: Fred's ImageMagick Tidbits http://www.fmwconcepts.com/imagemagick/tidbits/image.php#append

krubo
  • 5,969
  • 4
  • 37
  • 46