Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to get the predominant colour in an image from the command line? Ideally I'd like it back as hex or RGB.

I thought it might be possible to do this with imagemagick's identify command, but I couldn't see an option for doing this in the documentation.

share|improve this question
    
Could you be more specific about what is a predominant color for you? 24-bit RGB pixel has 16777216 possible color values and I doubt if finding the most repeating one is useful in any way. –  gronostaj Apr 2 '13 at 16:54
    
I'd like to narrow it down to a fairly small number of colours... I'm no expert on colours, but the sixteen HTML safe colours or the X11 colour names would be ideal. I guess this would need some kind of process like (i) reduce the colour space of the image (ii) create a table of pixels by colour (iii) map the dominant colour to the closest colour name. No idea how to do any of that, though! –  Richard Apr 2 '13 at 16:58

2 Answers 2

up vote 3 down vote accepted

identify -verbose provides a palette histogram table, but only if the number of unique colors is below a limit (1024 in the versions I've checked, v6.x up to v6.8.3).

Instead you can just "convert" your image to a histogram, this output is not limited by number of unique colors:

convert image.jpg  -format %c -depth 8  histogram:info:histogram_image.txt
sort -n histogram_image.txt | tail -1

Sorting the output numerically by the first column sorts by frequency of pixel colors. (Frequency of specific colour pixels might not correspond to a human perception of predominant color of course.)

If you're reducing colors, you can probably just trust convert to do it for you by your choice of dithering and/or posterizing. Even specifically to the web-safe 216:

convert image.png  +dither -remap netscape:  image_websafe.png

You would probably get closer to a perceived predominant color by some combination of blurring, resizing and posterizing, this is not a simple problem to define and solve:

share|improve this answer
    
Thanks - great answer and the first two commands actually work pretty well! One final question: what do you think is the best way to map the predominant hex value to the nearest websafe value? Is this just a question of hex arithmetic or is there a more sophisticated way? –  Richard Apr 2 '13 at 21:35
    
Given the palette is so limited, treating (R,G,B) as a 3d space and finding the minimal Euclidean distance to a defined colour would probably suffice for most purposes. You can get convert to do this for you by adding -remap netscape: after -depth 8 in the first command above. –  mr.spuratic Apr 2 '13 at 22:02
    
Awesome - thank you. –  Richard Apr 7 '13 at 11:19

I found this elegant and short answer here http://blog.endpoint.com/2011/04/determining-dominant-image-color.html

$ convert Waffle.jpg -scale 1x1\! -format '%[pixel:u]' info:-
rgb(219,166,94)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.