Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A ImageMagick Script Can Generate Color Palettes JSON Format. Usage: ./colors.sh IMAGE-PATH COLOR-NUMBERS eg: ./colors.sh /Users/Cyril/Downloads/photo2.jpg 10
dir="." #Set the default temp dir
tmpA1="$dir/spectrumhist_1_$$.png"
tmpB1="$dir/spectrumhist_1_$$.cache"
trap "rm -f $tmpA1 $tmpB1; exit 0" 0 #remove temp files
trap "rm -f $tmpA1 $tmpB1; exit 1" 1 2 3 15 #remove temp files
if [ $# -eq 2 ]
then
colors=$2
else
colors=8
fi
convert -quiet -regard-warnings "$1" -alpha off +dither -colors $colors -depth 8 +repage "$tmpA1"
convert "$tmpA1" -set colorspace scRGB -colorspace rgb -format %c -define histogram:unique-colors=true histogram:info:- | \
sed -n 's/^ *\(.*\): (\(.*,\)\(.*,\)\(.*\)) #\(.*\) rgb(\(.*\))$/\1 \2 \3 \4 \5 \6/p' | \
sort -n -k 1,1 | \
awk -v wd=10 -v mag=1 '
{ list=""; i=NR; count[i]=$1; rgb[i]=$2 $3 $4; hex[i]=$5; hsl[i]=$6 }
END { for (i=1; i<=NR; i++)
{ ht[i]=int(10000*count[i]/count[NR])/100; list="{\"counts\":\""count[i]"\",\"rgb\":\""rgb[i]"\",\"hex\":\""hex[i]"\"}" list; if(i<NR){list = ",\n" list}}
{list="{result:[" list "]}";print list; } } '
Owner

cyrilis commented Nov 24, 2014

A ImageMagick Script Can Generate Color Palettes JSON Format.

Usage :

./colors.sh IMAGE-PATH COLOR-NUMBERS 

eg:

./colors.sh /Users/Cyril/Downloads/photo2.jpg 10

Result:

{
    "result": [
        {
            "counts": "47671", 
            "rgb": "86,178,252", 
            "hex": "56B2FC"
        }, 
        {
            "counts": "39093", 
            "rgb": "28,41,20", 
            "hex": "1C2914"
        }, 
        {
            "counts": "38709", 
            "rgb": "48,87,22", 
            "hex": "305716"
        }, 
        {
            "counts": "27427", 
            "rgb": "75,105,41", 
            "hex": "4B6929"
        }, 
        {
            "counts": "25865", 
            "rgb": "218,228,236", 
            "hex": "DAE4EC"
        }, 
        {
            "counts": "21302", 
            "rgb": "98,146,48", 
            "hex": "629230"
        }, 
        {
            "counts": "20692", 
            "rgb": "158,191,212", 
            "hex": "9EBFD4"
        }, 
        {
            "counts": "20216", 
            "rgb": "80,96,84", 
            "hex": "506054"
        }, 
        {
            "counts": "19568", 
            "rgb": "132,133,98", 
            "hex": "848562"
        }, 
        {
            "counts": "9457", 
            "rgb": "102,176,226", 
            "hex": "66B0E2"
        }
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment