fengsp / color-thief-py

Grabs the dominant color or a representative color palette from an image. Uses Python and Pillow.
http://lokeshdhakar.com/projects/color-thief/
Other
1.02k stars 125 forks source link

Dominant color not working properly #17

Open Britman72 opened 4 years ago

Britman72 commented 4 years ago

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).

Mayvis commented 4 years ago

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)