Fortes


Scripting Photo Collages with ImageMagick

Automating something I don’t even do anymore

Ipanema Beach

Ipanema Beach Rio de Janeiro, Brazil

Back before everyone was working from home, I wrote about my Green Screen setup and shared a selection of daily screenshots from meetings. Now that everyone works from home, the joke is a bit tired (and I got lazy) so I no longer bother with the green screen. My co-workers can now see my office filled with three crates of pandemic-panic-purchased toilet paper that I’m still working my way through.

I never got around to automating the collection of action shots from that post. Zoom has an API but my feeble attention span didn’t last long enough to find a good way to programmatically capture a daily image. Manual screenshots were simple enough, though I did often forget due to the fact that I was born in the 1900s and it’s probably already past my bedtime.

I did, however, have a simple script for generating the 3x3 images from screenshots using ImageMagick. Much like exiftool, which I use extensively for photo management , ImageMagick is one of those programs that have been around forever and can be coaxed to do just about anything via arcane command-line flags.

Though it’s been years, I still don’t know what I’m doing when writing these things so I’ll share here in case I ever need it again:

#!/bin/bash
#
# Creates 3x3 montages of given images, saved to current directory
#
# Usage:
# $ ./create_montage.sh [files...]

main() {
  local resized_dir
  resized_dir=$(mktemp -d)

  local iter
  iter=0
  for filename in "$@"; do
    iter=$(expr $iter + 1)

    echo "Resizing $filename"
    convert "$filename" -resize x400 -gravity center -crop 600x400+0+0 +repage \
      "$resized_dir/$iter.png"

    if [[ $(expr $iter % 9) == 0 ]]; then
      echo "Creating montage $(expr $iter / 9)"
      montage "$resized_dir/*.png" -mode Concatenate \
        "montage-$(expr $iter / 9).jpg"
      rm "$resized_dir/*.png"
      echo "It's time $iter"
    fi
  done

  echo "Processed $iter collages"
  rm -rf "$resized_dir"
}

main $@

It’s not the prettiest, but it did the job. Or it least it used to. Maybe it still does? I guess we’ll never know.