Page 1 of 1

Getting the colors of an image as text/hex values

Posted: 2009-10-07T13:47:30-07:00
by qubodup
Hello,

I would like to generate a list of colors used in an image. kind of like

Code: Select all

convert in.png -unique-colors out.png
only that I get the output in text form, one line per color. I also would like this list to be sorted by how often the color appears in the image. Something like

Code: Select all

$ colors image.png
350 ffffff
34  ddffee
32  defecd
29  99ff99
22  994499
13  ffeef3
3   fefea1
I would be thankful for hints on solving any part of my problem here.

Thanks

Re: Getting the colors of an image as text/hex values

Posted: 2009-10-07T15:24:00-07:00
by fmw42
This will list out the top 10 colors in the histogram:

convert logo: -depth 8 -format %c histogram:info: | sort -r -k 1 | head -n 10 | sed -n 's/^\([ 0-9]*\): .*[#]\([a-zA-Z0-9]*\) .*$/\1 \2/p'

253350 FFFFFF
10877 025AA4
2244 31337D
2063 FBF6F5
1500 EA2B2C
1070 221C1B
1065 F1A57D
898 242021
862 1C191B
715 F9EA4D

If you want fewer than 256 colors in the histogram, add -colors XX.

If you want more than the top 10, then change the number after head -n XX

If you use -unique-colors, then the count will always be 1.

Re: Getting the colors of an image as text/hex values

Posted: 2009-10-07T17:26:11-07:00
by qubodup
fmw42 wrote:This will list out the top 10 colors in the histogram:

convert logo: -depth 8 -format %c histogram:info: | sort -r -k 1 | head -n 10 | sed -n 's/^\([ 0-9]*\): .*[#]\([a-zA-Z0-9]*\) .*$/\1 \2/p'

If you want fewer than 256 colors in the histogram, add -colors XX.

If you want more than the top 10, then change the number after head -n XX

If you use -unique-colors, then the count will always be 1.
Thank you very much! This is amazing! (-ly easy and handy)

Re: Getting the colors of an image as text/hex values

Posted: 2009-10-07T21:15:17-07:00
by fmw42
You can also do this to make the image have only 10 colors and list them.

convert logo: +dither -colors 10 -format %c histogram:info: | sort -r -k 1 | head -n 10 | sed -n 's/^\([ 0-9]*\): .*[#]\([a-zA-Z0-9]*\) .*$/\1 \2/p'

256454 FFFFFF
15159 0758A2
8098 201C1E
6402 E8BF5E
5560 313B87
4068 DF3834
3317 D5D2D3
2868 B2A49F
2842 444565
2432 698C87

Re: Getting the colors of an image as text/hex values

Posted: 2009-10-07T23:27:53-07:00
by qubodup
fmw42 wrote:You can also do this to make the image have only 10 colors and list them.

convert logo: +dither -colors 10 -format %c histogram:info: | sort -r -k 1 | head -n 10 | sed -n 's/^\([ 0-9]*\): .*[#]\([a-zA-Z0-9]*\) .*$/\1 \2/p'
That is the next thing I would be trying to do :) Thanks again!