Today, we are going to learn about an interesting topic - create a montage from images in Linux using ImageMagick suite. Using this method, you can create a composite image with a collection of random PNG or JPEG and other type of images. The ImageMagick program comes with a bunch of useful command line tools to do various tasks from command line. The one we are going to use now is called montage.
The montage command is used to create a composite image by combining several separate images. The images are tiled on the composite image. You can optionally decorate them with borders, frames, image name and more. It is originally designed for producing an array of thumbnail images. But it can do more than that. In this guide, let us learn how to create montages from command line in Linux.
Install ImageMagick On Linux
Since montage is part of the ImageMagick suite, make sure you have installed it on your Linux system. ImageMagick is available in the official repositories of popular Linux distributions.
On Arch Linux, Manjaro Linux:
$ sudo pacman -S imagemagick
On Debian, Ubuntu, Linux Mint:
$ sudo apt install imagemagick
On CentOS, RHEL:
$ sudo yum install epel-release
$ sudo yum install ImageMagick
$ sudo dnf install ImageMagick
$ sudo zypper install ImageMagick
Create A Montage From Images In Linux
I have the following four images in the ~/Pictures folder.
To create a montage from these images, run:
$ montage image1.png image2.png image3.png image4.png montage.png
Here, montage.png is the final output file.
This command will create the following montage:
If all images are same type, you can simply use the following command:
$ montage *.png montage.png
If the images are different type, use full name.
Not just PNG, you can create a montage from any type of images, for example GIF.
$ montage image1.gif image2.gif image3.gif image4.gif montage.png
It is also possible to create montage from different type of image formats.
$ montage image1.png image2.jpg image3.gif montage.png
Set image size and space between the images
The montage command has an option called "-geometry" which helps you to set the thumbnail size and the space between each image. The default -geometry setting is '120x120>+4+3'. Meaning - It will produce 120×120 thumbnails with 4 pixels on the left and right of each image and 3 pixels below. Here '>' is resize option. It is used to shrink only if the the size of the images are 'greater than' the size given.
As you can see in the above output, there is some space between top and bottom images in the montage. If you want to set 2 pixel spacing between the thumbnails, run:
$ montage -geometry +2+2 *.png montage.png
Now the montage will look like below:
This is useful when you try create a composite image from the images of same size.
What if you have the different size images? It is possible to resize all images into same size like below.
$ montage -geometry 100x100+2+2 *.png montage.png
Here 100x100 is the tile size. This setting will shrink the given images to fit into a box 100x100 pixel in size.
You can further restrict the resize to just images larger than the specified tile size like below.
$ montage -geometry 100x100\>+2+2 *.png montage.png
The thumbnails can be made to overlap if you set the spacing value in minus.
$ montage -geometry 100x100-10-2 *.png montage.png
Set tile layout
The another useful option of montage command is -tile. This option helps you to decide how the images to be laid out on the montage.
For example, if you want to lay down all images in a single row, run:
$ montage -tile x1 *.png montage.png
This command will generate a montage like below:
If there are five images, run:
$ montage -tile x3 image1.png image2.png image3.png image4.png image5.png montage.png
You can also combine -tile and -geometry options together and create montages like below.
$ montage -tile x3 -geometry 50x50+2+2 *.png montage.png
Create montage with Polaroid effect
To produce a montage with Polaroid effect, run:
$ montage +polaroid *.png montage.png
Make the images to overlap using command:
$ montage -geometry 100x100-10-2 +polaroid *.png montage.png
Rotate images in the montage
We can rotate the thumbnails by a particular degree, for example 30 degree, like below.
$ montage -rotate 30 *.png montage.png
Change background color of montage
By default, the Montage tool will set white background to the montages. You can however change this using -background option to set specific background color to the montage.
The following command will set light blue color to the montage background.
$ montage -background lightblue *.png montage.png
Or, use Hex color codes to set a custom color of your choice:
$ montage -background '#AE2F14' *.png montage.png
If you don't want any background color, simply use -background none option.
$ montage -background none *.png montage.png
Set background image
To set a custom background image for your montage, specify its path using -texture option:
$ montage -texture ~/ostechnix.png *.png montage.png
Set border, frame and shadow decoration
Montage tool can make the thumbnail images with a border, frame and shadow decoration.
To set a border around the images, simply do:
$ montage -border 5 *.png montage.png
After setting the border, the final montage will look below:
Notice the border around each thumbnail.
If the border is not clearly visible, set a custom color to the border like below.
$ montage -border 5 -bordercolor lightblue *.png montage.png
To set frame decoration, run:
$ montage -frame 5 *.png montage.png
Similarly, we can set shadow decoration, using command:
$ montage -shadow *.png montage.png
If the shadow is not clearly visible, remove or change the background color to make the shadow clearly visible.
Label montage images
With -set label option, we can tell the Montage tool to set labels for each thumbnail images.
$ montage -set label '%f' *.png montage.png
This command will label the thumbnail images with their source filenames.
It is also possible to include the dimensions of each image in their label names.
$ montage -set label '%f\n%wx%h' *.png montage.png
Can we set a custom name for each image? Of course, yes!
$ montage -label stay image1.png -label home image2.png -label stay image3.png -label safe image4.png montage.png
How about a title to the whole montage? Here you go!
$ montage -label stay image1.png -label home image2.png -label stay image3.png -label safe image4.png -title 'OSTechNix' montage.png
We can also set color for the text labels and titles.
$ montage -label stay image1.png -label home image2.png -label stay image3.png -label safe image4.png -title 'OSTechNix' -fill blue montage.png
The above command will set blue color for text labels and montage title.
This is another cool feature of Montage tool. With concatenate mode, we can join the thumbnail images together without any spaces.
The following command will concatenate the given images without any spaces in a single row layout.
$ montage -mode Concatenate -tile x1 *.png montage.png
To lay down the images in 2x2 layout without any spaces, run:
$ montage -mode Concatenate -tile 2x2 *.png montage.png
And, that's all. What we have seen so far are just the basic options. Montage tool has a lot of other other useful options. For more details, refer the official ImageMagick documentation given at the end.
Also refer man pages:
$ man montage
- How To Convert Images Into ASCII Format In Linux
- How To View Image Metadata On Linux
- How To Create Animated GIF In Linux
- How To Create A Video From PDF Files In Linux
- Save Linux Command Output To An Image
- 3 CLI Image Viewers To Display Images In The Terminal