Getting the colors of an image as text/hex values

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
qubodup
Posts: 31
Joined: 2009-10-07T13:11:41-07:00
Authentication code: 8675309

Getting the colors of an image as text/hex values

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
User avatar
qubodup
Posts: 31
Joined: 2009-10-07T13:11:41-07:00
Authentication code: 8675309

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

Post 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)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
User avatar
qubodup
Posts: 31
Joined: 2009-10-07T13:11:41-07:00
Authentication code: 8675309

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

Post 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!
Post Reply