Skip to content

Instantly share code, notes, and snippets.

@colehocking
Created April 3, 2026 23:10
Show Gist options
  • Select an option

  • Save colehocking/7aa5bb06436f68a0b3441be78f5e3045 to your computer and use it in GitHub Desktop.

Select an option

Save colehocking/7aa5bb06436f68a0b3441be78f5e3045 to your computer and use it in GitHub Desktop.
Convert an image to monochrome
#!/bin/bash
# Turn any image to monochrome
# -- Cole Hocking
# -----------------------------------------------------------------------------
# Verify Homebrew installation for package management
# Docs: https://brew.sh/
go_brew() {
echo "Checking for Homebrew package management tool..."
if [[ -f "$(which brew)" ]]; then
echo "Brew is installed!"
exit $?
else
echo "Homebrew is not installed. Please follow the prompts for installation."
echo "(requires sudo for installation)"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
# Confirm that brew is installed before we continue
sync
if [[ ! -f "$(which brew)" ]]; then
echo 'Brew does not installed or installation aborted. Re-run ./monochrome.sh ' > 2
exit 1
else
echo "Brew has been installed successfully!"
fi
}
# -----------------------------------------------------------------------------
# verify imagemagick installation
go_magick() {
echo "Checking for ImageMagick..."
if [[ -f "$(which magick)" ]]; then
echo "ImageMagick is installed!"
else
echo "ImageMagick is not installed. Installing..."
brew install imagemagick
fi
# Confirm that brew is installed before we continue
sync
if [[ ! -f "$(which magick)" ]]; then
echo 'ImageMagick does not installed or installation aborted. Re-run ./monochrome.sh ' > 2
exit 1
else
echo "ImageMagick has been installed successfully!"
fi
}
# -----------------------------------------------------------------------------
# main call
main() {
# check that infile/outfile supplied
if [[ $# -ne 2 ]]; then
echo "Usage: ./monochrome.sh <input_file.jpg> <output_file.png>"
exit(1)
else
# set input file
INFILE="$1"
# set output file
OUTFILE="$2"
fi
# check for homebrew
go_brew
# check for imagemagick
go_magick
# convert the images
magick "${INFILE}" -monochrome "${OUTFILE}"
echo "Image converted to: ${OUTFILE}"
echo "Done!"
}
# call main
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment