Created
April 14, 2026 22:27
-
-
Save colehocking/17e9d8e7b855504e3b9179b1014db103 to your computer and use it in GitHub Desktop.
Turn a jpg to png and make the background transparent
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 a jpg to png & make the background transparent | |
| # Can also be png to png | |
| # -- 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!" | |
| 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 "Successfully Brewed Up." | |
| 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 ./transparentpng.sh ' > 2 | |
| exit 1 | |
| else | |
| echo "Prereqs Complete." | |
| fi | |
| } | |
| # ----------------------------------------------------------------------------- | |
| # main call | |
| main() { | |
| # check that infile/outfile & background color supplied | |
| if [[ $# -ne 3 ]]; then | |
| echo "Usage: ./transparentpng.sh <input_file.jpg> <output_file.png> <bkg_color>" | |
| exit(1) | |
| else | |
| # set input file | |
| INFILE="$1" | |
| # set output file | |
| OUTFILE="$2" | |
| # set background color | |
| BKG_COLOR="$3" | |
| fi | |
| # check for homebrew | |
| go_brew | |
| # check for imagemagick | |
| go_magick | |
| # convert the image; adjust fuzz factor as needed | |
| magick "${INFILE}" -fuzz 10% -transparent "${BKG_COLOR}" "${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