Skip to content

Instantly share code, notes, and snippets.

@varunchandak
Created September 30, 2025 08:11
Show Gist options
  • Save varunchandak/666bfce6ae9485afb38c87fa055b0f7b to your computer and use it in GitHub Desktop.
Save varunchandak/666bfce6ae9485afb38c87fa055b0f7b to your computer and use it in GitHub Desktop.

πŸ–ΌοΈ Collage Script

collage.sh is a simple command-line tool to generate a uniform photo collage from all images in the current directory.
It uses ImageMagick under the hood (montage command).


✨ Features

  • Works with JPG/JPEG/PNG images (case-insensitive).
  • Automatically detects the number of images and creates a near-square grid.
  • Resizes all images to uniform cells while preserving the full image (adds padding as needed).
  • Adjustable thumbnail size and output filename via CLI arguments.
  • Default background color is white (can be changed in the script).

πŸ“¦ Requirements

Install on:

  • macOS:
    brew install imagemagick
  • Ubuntu/Debian:

    sudo apt-get install imagemagick

πŸš€ Usage

  1. Save the script as collage.sh

  2. Make it executable:

    chmod +x collage.sh
  3. Run it in a folder containing your images:

    ./collage.sh

Arguments

./collage.sh [SIZE] [OUTPUT]
  • SIZE β†’ (optional) cell size for each image, e.g. 300x300 (default: 300x300).
  • OUTPUT β†’ (optional) output file name (default: collage.jpg).

Examples

  • Default:

    ./collage.sh

    β†’ Creates collage.jpg with 300Γ—300 image cells.

  • Custom size:

    ./collage.sh 400x400

    β†’ Creates collage.jpg with 400Γ—400 cells.

  • Custom size + output filename:

    ./collage.sh 400x400 mycollage.png

    β†’ Creates mycollage.png with 400Γ—400 cells.


πŸ“ Notes

  • The grid size is automatically chosen based on the number of images.
  • Images are resized to fit inside the chosen SIZE while keeping their full content visible.
  • Empty space is padded (default: white). Change -background white in the script if you prefer black or transparent.

βœ… Example Output

If you have 9 images in a directory and run:

./collage.sh 300x300 collage.jpg

You’ll get a neat 3Γ—3 collage, with each image scaled to fit a 300Γ—300 cell.


πŸ“œ License

MIT – feel free to use and modify.

#!/usr/bin/env bash
# collage.sh - Create a uniform collage of images in the current directory
# Requires: ImageMagick (montage command)
set -euo pipefail
# Default values
SIZE="${1:-300x300}" # Thumbnail size (each cell)
OUTPUT="${2:-collage.jpg}" # Output file
# Find all images (jpg, jpeg, png) in current dir
IMAGES=(*.{jpg,jpeg,png,JPG,JPEG,PNG})
if [ ${#IMAGES[@]} -eq 0 ]; then
echo "❌ No images found in the current directory."
exit 1
fi
# Count images
COUNT=${#IMAGES[@]}
# Compute grid size (near-square)
COLS=$(echo "sqrt($COUNT)" | bc)
ROWS=$(( (COUNT + COLS - 1) / COLS ))
echo "πŸ“Έ Creating collage of $COUNT images..."
echo " Grid: ${COLS}x${ROWS}, Cell size: $SIZE, Output: $OUTPUT"
montage "${IMAGES[@]}" \
-resize "$SIZE" \
-background white -gravity center -extent "$SIZE" \
-tile ${COLS}x${ROWS} -geometry +2+2 \
"$OUTPUT"
echo "βœ… Collage created: $OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment