Create image collages with ImageMagick CLI

Creating image collages is a great way to combine multiple photos into a single, eye-catching composition—ideal for portfolios, event highlights, or gallery displays. In this post, we will explore how to use ImageMagick's command-line tools to automate collage creation, customize layouts, and process multiple directories in batch.
Set up ImageMagick
First, install ImageMagick on your system. For Ubuntu/Debian:
sudo apt-get update
sudo apt-get install imagemagick
Verify your installation and check the version:
# For ImageMagick 7
magick --version
# For ImageMagick 6
convert --version
Note: Commands in this guide work with both ImageMagick 6 and 7. For version 7, replace convert
with magick
.
Configure resource limits
Before processing large batches, adjust ImageMagick's resource limits to prevent memory issues:
# Check current limits
identify -list resource
# Edit policy file
sudo nano /etc/ImageMagick-6/policy.xml # or /etc/ImageMagick-7/policy.xml
Adjust these values based on your system's capabilities:
<policy domain="resource" name="memory" value="256MiB"/>
<policy domain="resource" name="disk" value="1GiB"/>
Prepare your images
For consistent collages, normalize your image sizes. Here are the commands for both ImageMagick versions:
# ImageMagick 6
convert input.jpg -resize 200x200 resized.jpg
# ImageMagick 7
magick input.jpg -resize 200x200 resized.jpg
To process multiple images:
for img in *.jpg; do
magick "$img" -resize 200x200 "resized_$img"
done
Create a collage using montage
The montage
command combines images into a grid layout. Basic usage:
montage image1.jpg image2.jpg image3.jpg -tile 3x1 -geometry +2+2 collage.jpg
Command options explained:
-tile 3x1
: Three columns, one row-geometry +2+2
: 2-pixel spacing between images
Customize your layout
Create more sophisticated collages with these options:
# Add background color and frame
montage *.jpg -tile 3x -geometry +4+4 -frame 5 \
-background '#336699' framed_collage.jpg
# Create polaroid-style layout
montage *.jpg -tile 3x -geometry +4+4 -shadow \
-background white -polaroid 0 polaroid_collage.jpg
# Add title to each image
montage *.jpg -title '%f' -tile 3x -geometry +4+4 \
-background '#f0f0f0' titled_collage.jpg
Batch processing with error handling
This script processes multiple directories with error handling and logging:
#!/bin/bash
# Set up logging
exec 1> >(tee "collage_process.log") 2>&1
for dir in */; do
if [ -d "$dir" ]; then
echo "Processing directory: $dir"
# Count JPG files
count=$(find "$dir" -maxdepth 1 -type f -name "*.jpg" | wc -l)
if [ $count -eq 0 ]; then
echo "No JPG files found in $dir"
continue
fi
# Create collage with error handling
montage "$dir"*.jpg -tile 3x -geometry +2+2 \
"${dir%/}-collage.jpg" 2>/dev/null || {
echo "Error creating collage for $dir"
continue
}
echo "Successfully created collage for $dir"
fi
done
Troubleshooting
Common issues and solutions:
- If montage fails with "unable to open image", verify file paths and permissions
- For "no decode delegate" errors, install additional format support:
sudo apt-get install libheif-dev libjpeg-dev libpng-dev
- For memory errors, adjust resource limits as shown in the configuration section
Conclusion
ImageMagick's CLI tools provide powerful options for creating custom image collages efficiently. By combining these commands with shell scripts, you can automate the creation of professional-looking collages for various purposes. For advanced image processing needs, consider exploring Transloadit's image-manipulation-service, which builds upon ImageMagick's capabilities with additional features and cloud-based processing.