Skip to content

Instantly share code, notes, and snippets.

@slowkow
Created March 21, 2023 22:52
Show Gist options
  • Save slowkow/49a0df383749ba0c8396496ab3a872ea to your computer and use it in GitHub Desktop.
Save slowkow/49a0df383749ba0c8396496ab3a872ea to your computer and use it in GitHub Desktop.
Make a montage of circle-cropped photos

Make a montage of circle-cropped photos

Here are the source files, the masked output folder, and the montage.png output file:

Here are the images in the masked output folder:

Here is a Bash script you can save to a file on your computer called circles.sh:

#!/usr/bin/env bash
#
# Usage
# -----
#
#     bash circles.sh src/*.png src/*.jpg
#
# Writes a file "montage.png" to the current directory.
#
#
# Dependencies
# ------------
#
# On macOS: brew install imagemagick
# On ubuntu: sudo apt install imagemagick
#

for f in "$@"
do
  # skip non-files
  if [[ ! -f $f ]]
  then
    continue
  fi

  input=${f}
  mkdir -p masked
  output=masked/$(basename ${input%.*}).png

  # image width and height
  w=$(identify -format '%w' $input)
  h=$(identify -format '%h' $input)
  # set circle radius to half of the width
  r=$((w / 2))

  # https://stackoverflow.com/questions/41959355
  # mask a circle on the input image
  convert $input \
          -gravity Center \
          \( -size ${w}x${w} \
             xc:Black \
             -fill White \
             -draw "circle $r $r $r 1" \
             -alpha Copy \
          \) -compose CopyOpacity -composite \
          -trim $output
done

# -tile {c}x{r}
# -geometry {w}x{h}+{xoffset}+{yoffset}
montage $(ls masked/*.png) -tile 4x -geometry 200x200+10+10 montage.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment