Skip to content

Instantly share code, notes, and snippets.

@rob-gordon
Last active May 15, 2017 22:18
Show Gist options
  • Select an option

  • Save rob-gordon/dfd804af3c12b01e10a4c634270b9167 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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