Last active
May 15, 2017 22:18
-
-
Save rob-gordon/dfd804af3c12b01e10a4c634270b9167 to your computer and use it in GitHub Desktop.
use imageMagick and gsed to save count,hex of colors in an image
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 | |
| # Return the histogram | |
| fileToWrite="./colors.csv" | |
| filename="" | |
| regex='(\d+): ' | |
| if [ "$1" != "" ]; then # Test for positional argument | |
| if [ -f "$1" ]; then # Test if regular file, not directory | |
| filename="$1" | |
| # Test if a valid file type | |
| if [[ $filename == *.png ]] || [[ $filename == *.jpg ]]; then | |
| # erase the file | |
| > $fileToWrite | |
| # create histogram | |
| colors=$(convert "$filename" -define histogram:unique-colors=true -format %c histogram:info:-) | |
| # parse histogram | |
| gsed -rn 's/[ ]+([[:digit:]]+): \([ 0-9]{3},[ 0-9]{3},[ 0-9]{3},[ 0-9]{3}\) (#[A-F0-9]{8}) [A-Za-z0-9\(\),]+$/\1,\2/p' <<< "$colors" > $fileToWrite | |
| else | |
| echo "Error! Not a valid file type" 1>&2 | |
| exit 64; | |
| fi | |
| else | |
| echo "Error! Not a valid file." 1>&2 | |
| exit 64; | |
| fi | |
| else | |
| echo "Error! No file passed in." 1>&2 | |
| exit 64; | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment