color-thief-py
color-thief-py copied to clipboard
Dominant color not working properly
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).
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)