Last active
December 12, 2023 06:27
-
-
Save neckothy/e59b98d489c905813a13610ec0998509 to your computer and use it in GitHub Desktop.
Automatically grayscale images which fall under a set saturation threshold
This file contains 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 | |
# https://www.imagemagick.org/discourse-server/viewtopic.php?t=19580#p81226 | |
# https://unix.stackexchange.com/a/9499 | |
# http://www.fmwconcepts.com/imagemagick/tidbits/image_info.php#gray | |
find . -type f \( -name '*.jpg' -o -name '*.png' \) -exec sh -c ' | |
for file do | |
graytest=$(identify -format "%[colorspace]" "$file") | |
if [[ $graytest != Gray ]]; then | |
sattest=$(convert "$file" -colorspace HSL -channel g -separate +channel -format "%[fx:mean<.12?1:0]" info:) | |
if [[ $sattest -eq 1 ]]; then | |
[[ $file == *.jpg ]] && echo "$file: converting to grayscale" && jpegtran -grayscale -outfile "$file" "$file" | |
[[ $file == *.png ]] && echo "$file: converting to grayscale" && convert "$file" -colorspace Gray "$file" | |
else | |
echo "$file: skipping, exceeds saturation threshold" | |
fi | |
else | |
echo "$file: skipping, colorspace is already Gray" | |
fi | |
done | |
' exec-sh {} + |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment