Skip to content

Instantly share code, notes, and snippets.

@beaulac
Last active September 27, 2018 14:26
Show Gist options
  • Save beaulac/a9ca6971ba1dd9fff33a3390ccb0574d to your computer and use it in GitHub Desktop.
Save beaulac/a9ca6971ba1dd9fff33a3390ccb0574d to your computer and use it in GitHub Desktop.
Replace fuzzy single color in sRGB transparent PNG
#!/usr/bin/env bash
FILE=$(realpath ${1});
OLD=$2;
NEW=$3;
# Default fuzz: 25%
FUZZ=${4:-25};
########################
# 1. Flatten & color-replace original into clone
# 2. Extract alpha channel from original image into clone
# 3. Remove original, composite flat + alpha into output.
########################
convert ${FILE} \
\( -clone 0 -alpha off -set colorspace sRGB -fuzz ${FUZZ}% -fill "${NEW}" -opaque "${OLD}" \) \
\( -clone 0 -alpha extract -set colorspace sRGB \) \
-delete 0 -compose copy_opacity -composite ${FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment