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.04k stars 125 forks source link

About complete white image having values (254,254,254) #25

Open pd2871 opened 3 years ago

pd2871 commented 3 years ago

The code will not be able to detect complete dominant white images with values like (254,254,254).

Please modify the code of get_palette() to:

r, g, b, a = pixels[i]
            # If pixel is mostly opaque and not white
            if a >= 125:
                if not (r > 255 and g > 255 and b > 255):
                    valid_pixels.append((r, g, b))

Setting the threshold of 250 is causing the problem

suzukieng commented 2 years ago

Or at least make it configurable:

old:
def get_palette(self, color_count=10, quality=10):

new:
def get_palette(self, color_count=10, quality=10, ignore_white=True):

and only do the thresholding if ignore_white is True.

suzukieng commented 2 years ago

There's already a PR for this: https://github.com/fengsp/color-thief-py/pull/11