skip to content

Combining images with ImageMagick

Assorted ImageMagick commands for combining images

Combining images vertically with a line separating them

This is useful for combining a few images into a single vertical strip, with a 10px white border between each image, like the portrait photos from photo boots.

convert *.png[400x400] -splice 0x10 \n    -background "#ffffff"  -append  -crop -0+10 output.png
convert
calls up one of the ImageMagick commands, convert
*.png[400x400]
an expression using wildcards to match all png images in the current folder, and resize each as it is read so that neither width and height are greater than 400px
-splice 0x10
add a 10px vertical border to each image
-background "#ffffff"
make that border white (using HTML color ocde)
-append
combine images vertically - use +append to combine them horizontally
-crop -0+10
crop the top 10 pixels from the combined image, as we want the borders only between images
output.png
this is the result image file. It could just as easily be a .jpg

Removing window artefacts from the top of the images first

If you have, say, a bunch of screenshots with the address bar etc at the top, you need to modify the command to get rid of it first. This time you can’t use the shortcut resize.

convert *.png -gravity south -splice 0x111 \n    -shave 0x111 -resize 400x400  -splice 0x10 -background "#ffffff"  \n    -append  -crop -0+10 output.png
convert
calls up one of the ImageMagick commands, convert
*.png
an expression using wildcards to match all png images in the current folder
-gravity south
aligns the next command(s) bottom
-splice 0x111
because I need to remove 111px from the top of the image (that's the Chrome toolbar on a Mac - different values would apply for different situations), it turns out to be easier to add the same amount to the bottom, then deal with top and bottom as one with the next command
-shave 0x111
This removes 111px from the top and bottom; -shave is easier to work with than -crop
-resize 400x400
now i can resize so that the image does not exceed 400 px either direction. From now on carry on as for previous command
-splice 0x10
add a 10px vertical border to each image
-background "#ffffff"
make that border white (using HTML color ocde)
-append
combine images vertically - use +append to combine them horizontally
-crop -0+10
crop the top 10 pixels from the combined image, as we want the borders only between images
output.png
this is the result image file. It could just as easily be a .jpg

Combining images into an animated gif

If instead you’d rather create an animated gif, here’s the simplest way to do it - without any of the powerful options that ImageMagick offers.

convert *.png -gravity south -splice 0x111 \n    -shave 0x111 -resize 400x400  -set delay 300 output.gif
convert
calls up one of the ImageMagick commands,
*.png
an expression using wildcards to match all png images in the current folder
-gravity south
aligns the next command(s) bottom
-splice 0x111
because I need to remove 111px from the top of the image (that's the Chrome toolbar on a Mac - different values would apply for different situations), it turns out to be easier to add the same amount to the bottom, then deal with top and bottom as one with the next command
-shave 0x111
This removes 111px from the top and bottom; -shave is easier to work with than -crop
-resize 400x400
now i can resize so that the image does not exceed 400 px either direction
-set delay 300
adds a three seconds delay between all images
output.gif
that's it - because a list of images and a delay were supplied, ImageMagick automatically converts to an animated gif.

Cropping a set of images to the same size and animating them

If the images in the gif are of different size you may want to do something about that. Here they are cropped to a minimum common size, the extra pixels discarded.

convert *.png -resize 800x500^ -gravity center \n    -crop  800x500+0+0 +repage -set delay 300 output.gif
convert
calls up one of the ImageMagick commands,
*.png
an expression using wildcards to match all png images in the current folder
-resize 800x500^
the ^ after the dimensions means those are the minimum sizes rather then the maximum
-gravity center
aligns the next command(s)
-crop 800x500+0+0
crops to 800x500, with no offest (0,0)
+repage
resets the origin after the crop
-set delay 300
adds a three seconds delay between all images
output.gif
that's it - because a list of images and a delay were supplied, ImageMagick automatically converts to an animated gif.

Arranging images in a grid

This is actually two commands, one after the other - one to resize and crop the images, the other to arrange them.

convert *.png -gravity south \n    -splice 0x111 -shave 0x111 -resize 400x400 converted.png
montage converted*.png -mode concatenate  -tile 2x2  output.png
convert
calls up one of the ImageMagick commands, convert
*.png
an expression using wildcards to match all png images in the current folder
-gravity south
aligns the next command(s) bottom
-splice 0x111
because I need to remove 111px from the top of the image (that's the Chrome toolbar on a Mac - different values would apply for different situations), it turns out to be easier to add the same amount to the bottom, then deal with top and bottom as one with the next command
-shave 0x111
This removes 111px from the top and bottom; -shave is easier to work with than -crop
-resize 400x400
now i can resize so that the image does not exceed 400 px either direction
converted.png
for each input image, it creates a correspoding output image in the current folder named converted-1.png, converted-2.png...
montage
calls up one of the ImageMagick commands, montage
converted*.png
this time only match the images whose name start with 'converted', i.e. the ones created by the previous command
-mode concatenate
arranges them in a grid
-tile 2x2
in this particular case, it is a 2x2 grid as I only had four images. You can use x2 or 2x to keep the number of rows and columns respectively fixed at 2, and the other dimension filled in with however many images you have
output.png
...and here comes the image

More

More ImageMagick commands.