fritz stelluto
development for digital media
berlin, london

Combining images with ImageMagickGeekery

Assorted ImageMagick commands to combine images (I use them for screenshots of interesting sites). Latest: arranging images in a grid

Combining images vertically with a line separating them

This is useful to combine 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 -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 -shave 0x111 -resize 400x400 -splice 0x10 -background "#ffffff" -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 -shave 0x111 -resize 400x400 -set delay 300 output.gif

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
-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 -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 ImageMagick commands.

2 comments to "Combining images with ImageMagick"

  1. Is there a way to combine 2 web-hosted images in a browser address bar so that they are joined images at the single web address?

  2. Not from a browser address bar, unless you find a public script on the internet that does exactly what you are asking for. In which case you’ll have to ask the author of the script how to use it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strong>