Created
April 3, 2026 23:10
-
-
Save colehocking/7aa5bb06436f68a0b3441be78f5e3045 to your computer and use it in GitHub Desktop.
Convert an image to monochrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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