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

Return the percentage the color covers in the image

Open jyotiska opened this issue 10 years ago • 6 comments

Hi,

I am playing with this for a last couple of days. Is it possible to return the percentage each color covers in a given image?

jyotiska avatar May 18 '15 10:05 jyotiska

I'm also interested in this data. Is there another library that can do it?

richardcornish avatar Aug 30 '15 00:08 richardcornish

I am also interested in the same data @jyotiska @richardcornish Please let me know if it's possible

amitmadan18 avatar Feb 22 '16 12:02 amitmadan18

If @jyotiska, @amitmadan18, or anybody else needs this information, I figured it out with the help of an issue at the original color-thief repo.

Change the palette method at line 356 in colorthief.py from:

def palette(self):
    return self.vboxes.map(lambda x: x['color'])

to:

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

I simply added additional members to each RGB tuple:

  • The pixel count of each color
  • The total sum of all pixels, which will always be the same
  • The integer percentage of each color

Now palette = color_thief.get_palette(color_count=3) will return something like [(236, 70, 70, 1875, 1000, 18), (4, 4, 252, 625, 1000, 6), (4, 4, 4, 7500, 1000, 75)], giving 18%, 6%, and 75%. Tweak to your liking. Fork the repo to make your change.

richardcornish avatar Jun 13 '16 19:06 richardcornish

Correct. If I remember correctly, I also came up with my own hack something like this to print out the histogram.

jyotiska avatar Jun 14 '16 06:06 jyotiska

Thanks. Even I had came up with the same solution :) On 14 Jun 2016 11:34, "Jyotiska Khasnabish" [email protected] wrote:

Correct. If I remember correctly, I also came up with my own hack something like this to print out the histogram.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/fengsp/color-thief-py/issues/1#issuecomment-225787844, or mute the thread https://github.com/notifications/unsubscribe/AHC3hFI2OnmsJS8iskJNADXIIsJNEbpyks5qLkRWgaJpZM4Eepfi .

amitmadan18 avatar Jun 14 '16 07:06 amitmadan18

Is is comming to the main package or do I need to make a fork myself?

raoulspronck avatar Jan 27 '24 14:01 raoulspronck