color-thief-py icon indicating copy to clipboard operation
color-thief-py copied to clipboard

Dominant color not working properly

Open Britman72 opened this issue 5 years ago • 1 comments

Take the following image: https://ibb.co/R3dnBpz

Calling using: dominant_color = color_thief.get_color(quality=1) The dominant color is clearly yellow (244, 237, 12) but it returns (28, 51, 49).

Britman72 avatar Jan 30 '20 16:01 Britman72

You should see the doc that get_color function mentioned, the bigger the number, the faster a color will be returned but the greater the likelihood that it will not be the visually most dominant color.

So the best way to get dominant color is to change class CMap's palette function in colorthief.py like below

def palette(self):
        total = sum(self.vboxes.map(lambda x: x['vbox'].count))
        return self.vboxes.map(
            lambda x: x['color'] + (x['vbox'].count, total, int(x['vbox'].count / float(total) * 100)))

And use it like below

from colorthief import ColorThief

color_thief = ColorThief('./assets/image/image3.png')  # change to your image path
palette = color_thief.get_palette(color_count=6)
dominant_color = max(palette, key=lambda i: i[-1])[0:3]  # (r, g, b)

print(dominant_color) 
print(palette)

Mayvis avatar Feb 05 '20 16:02 Mayvis