shime / dominant-color

Gets dominant color of an image.
MIT License
64 stars 0 forks source link

This package currently calculates the average color, not the dominant color. #2

Open mceachen opened 7 years ago

mceachen commented 7 years ago

Average colors for most images are muddy brown.

The dominant color for an image is found by extracting the most-used color from a reduced-palette (or "quantized") version of an image.

We'd expect green to be the dominant color for this image: image

but

$ convert test.jpg -scale '1x1!' -format '%[pixel:u]' info:-
srgb(108,107,83)

image

Quantizing the palette to 32, and looking for the most-seen color, though, produces

$ convert test.jpg -scale '32x32' +dither -colors 32 -define histogram:unique-colors=true -format "%c" histogram:info: | sort -nr | head -1 | awk '{ print $4 }'
srgb(135,152,101)

image

Much better!

Here's the output without the sorting and awking:

$ convert test.jpg -scale '32x32' +dither -colors 32 -define histogram:unique-colors=true -format "%c" histogram:info:
        51: ( 23, 24, 25) #171819 srgb(23,24,25)
        12: ( 29, 30, 34) #1D1E22 srgb(29,30,34)
        24: ( 30, 34, 38) #1E2226 srgb(30,34,38)
        50: ( 44, 44, 44) #2C2C2C srgb(44,44,44)
        18: ( 74, 56, 53) #4A3835 srgb(74,56,53)
        21: ( 81, 76, 55) #514C37 srgb(81,76,55)
        35: ( 86, 76, 72) #564C48 srgb(86,76,72)
         1: ( 92, 40, 52) #5C2834 srgb(92,40,52)
         3: ( 95, 62, 65) #5F3E41 srgb(95,62,65)
        16: ( 97,104, 81) #616851 srgb(97,104,81)
        12: (103, 83, 60) #67533C srgb(103,83,60)
        34: (106, 85, 77) #6A554D srgb(106,85,77)
        26: (109,131, 83) #6D8353 srgb(109,131,83)
        43: (111,107, 81) #6F6B51 srgb(111,107,81)
        37: (118,138, 89) #768A59 srgb(118,138,89)
        25: (121,110,104) #796E68 srgb(121,110,104)
        20: (123,143, 97) #7B8F61 srgb(123,143,97)
        50: (124,146, 92) #7C925C srgb(124,146,92)
        26: (130,147, 94) #82935E srgb(130,147,94)
        21: (133,109, 88) #856D58 srgb(133,109,88)
         1: (133, 55, 60) #85373C srgb(133,55,60)
       102: (135,152,101) #879865 srgb(135,152,101)
        47: (143,120,107) #8F786B srgb(143,120,107)
        17: (149,161,118) #95A176 srgb(149,161,118)
        18: (149,140,113) #958C71 srgb(149,140,113)
         6: (149,100, 89) #956459 srgb(149,100,89)
        29: (162,137,119) #A28977 srgb(162,137,119)
        20: (169,149,142) #A9958E srgb(169,149,142)
         3: (202,193,195) #CAC1C3 srgb(202,193,195)

There's almost no additional expense to doing it "correctly", also. The first command ran in 0.030s, the the second (correct) command ran in 0.036s.

coverboy commented 6 years ago

Hi, Thanks for your great convert with optioins!!. If I have to look up head -4, awk '{ print $4 }' doesn't work. (first line only applied.) So, how about changing last word from line awk '{ print $NF}'