Is there a way to identify colors of images?

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
sichen1234
Posts: 2
Joined: 2013-02-20T12:44:58-07:00
Authentication code: 6789

Is there a way to identify colors of images?

Post by sichen1234 »

Hello everybody,

Is there a way to identify the colors in an image?

For example, in the rose image example from http://www.imagemagick.org/Usage/color_basics/, is there a way to use ImageMagick to find out that the dominant color in the image is red, followed by green?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is there a way to identify colors of images?

Post by snibgo »

One approach:

Code: Select all

convert rose: -posterize 3 -define histogram:unique-colors=true -format "%c" histogram:info:-
       447: (  0,  0,  0) #000000 black
         3: (  0,  0,127) #00007F srgb(0,0,127)
        52: (  0,127,  0) #007F00 srgb(0,127,0)
         2: (  0,127,127) #007F7F srgb(0,127,127)
       468: (127,  0,  0) #7F0000 srgb(127,0,0)
         6: (127,  0,127) #7F007F srgb(127,0,127)
       317: (127,127,  0) #7F7F00 srgb(127,127,0)
       740: (127,127,127) #7F7F7F srgb(127,127,127)
        67: (127,127,255) #7F7FFF srgb(127,127,255)
         8: (127,255,127) #7FFF7F srgb(127,255,127)
         2: (127,255,255) #7FFFFF srgb(127,255,255)
       477: (255,  0,  0) #FF0000 red
        50: (255,  0,127) #FF007F srgb(255,0,127)
        76: (255,127,  0) #FF7F00 srgb(255,127,0)
       289: (255,127,127) #FF7F7F srgb(255,127,127)
        11: (255,255,127) #FFFF7F srgb(255,255,127)
       205: (255,255,255) #FFFFFF white
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is there a way to identify colors of images?

Post by fmw42 »

limit the number of colors in your image via -colors, then generate the histogram information and pipe it to sort (on unix)

For example:

convert rose: -colors 32 -format "%c" histogram:info:- | sort -k 1 -r

Code: Select all

       319: ( 51, 51, 45) #33332D srgb(51,51,45)
       232: (209, 57, 45) #D1392D srgb(209,57,45)
       203: (237, 52, 52) #ED3434 srgb(237,52,52)
       165: (209, 67, 58) #D1433A srgb(209,67,58)
       164: (232, 81, 89) #E85159 srgb(232,81,89)
       153: (115, 57, 47) #73392F srgb(115,57,47)
       145: (252,251,248) #FCFBF8 srgb(252,251,248)
       143: (111,147, 87) #6F9357 srgb(111,147,87)
       142: (106,102, 83) #6A6653 srgb(106,102,83)
       134: ( 57, 68, 51) #394433 srgb(57,68,51)
       129: ( 71, 57, 48) #473930 srgb(71,57,48)
       114: (178, 58, 43) #B23A2B srgb(178,58,43)
       113: ( 80, 75, 55) #504B37 srgb(80,75,55)
       101: ( 96, 95, 82) #605F52 srgb(96,95,82)
        99: (141, 60, 45) #8D3C2D srgb(141,60,45)
        79: ( 93,132, 62) #5D843E srgb(93,132,62)
        77: (224,220,210) #E0DCD2 srgb(224,220,210)
        70: (244, 58, 80) #F43A50 srgb(244,58,80)
        64: (154, 88, 81) #9A5851 srgb(154,88,81)
        62: (111,112,167) #6F70A7 srgb(111,112,167)
        61: ( 63, 61, 85) #3F3D55 srgb(63,61,85)
        60: ( 98, 84, 57) #625439 srgb(98,84,57)
        57: (152,153,225) #9899E1 srgb(152,153,225)
        53: (162, 67, 52) #A24334 srgb(162,67,52)
        50: (148,160,116) #94A074 srgb(148,160,116)
        47: ( 43, 47, 30) #2B2F1E srgb(43,47,30)
        40: (188,121,156) #BC799C srgb(188,121,156)
        39: (176,173,161) #B0ADA1 srgb(176,173,161)
        35: (145, 52, 29) #91341D srgb(145,52,29)
        31: (125,141,165) #7D8DA5 srgb(125,141,165)
        24: (182, 61, 69) #B63D45 srgb(182,61,69)
        15: (255, 27, 56) #FF1B38 srgb(255,27,56)
or to get the 10 most frequent colors (out of 256 colors)

convert rose: -colors 256 -format "%c" histogram:info:- | sort -k 1 -r | head -n 10

Code: Select all

        87: (254,254,253) #FEFEFD srgb(254,254,253)
        62: (106, 99, 84) #6A6354 srgb(106,99,84)
        49: ( 92, 77, 60) #5C4D3C srgb(92,77,60)
        46: ( 55, 44, 42) #372C2A srgb(55,44,42)
        46: ( 50, 45, 43) #322D2B srgb(50,45,43)
        45: ( 52, 52, 49) #343431 srgb(52,52,49)
        40: (104, 94, 85) #685E55 srgb(104,94,85)
        38: ( 45, 50, 44) #2D322C srgb(45,50,44)
        37: (199, 55, 37) #C73725 srgb(199,55,37)
        37: ( 67, 57, 53) #433935 srgb(67,57,53)
sichen1234
Posts: 2
Joined: 2013-02-20T12:44:58-07:00
Authentication code: 6789

Re: Is there a way to identify colors of images?

Post by sichen1234 »

Thanks for the reply. This is really helpful. But why are the histograms from @snibgo and @fmw42 so different? I can see @snibgo's histogram shows colors which are mostly red, but how should I read @fmw42's histogram? If these are the top colors, they don't look like (to my eye) the red in the rose.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is there a way to identify colors of images?

Post by snibgo »

fmw42 and I have used two different methods of reducing the number of colours in the image. See the documentation for the commands. In a nutshell: "-posterize" picks colours that are evenly distributed across the colourspace, whereas "-colors" picks colours that are useful within this particular image.

The colours in fmw's histogram like (209, 57, 45) are in the rose. If you don't believe this, look at the image using the eyedropper in Gimp, or some other tool.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is there a way to identify colors of images?

Post by fmw42 »

It also depends upon how many colors you limit the image to. In my second histogram I took the top 10 colors out of 256 which might not even include a close to red color, because the red is not uniformly red, but may vary quite a bit so that you have lots of different red values that have low counts in comparison to white or near black in the image. Thus limiting the colors to only a few will group the reds together in one manner of the other.
Post Reply