Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to filter out very light or dark colors #5

Open
EmilStenstrom opened this issue Jan 6, 2020 · 4 comments
Open

A way to filter out very light or dark colors #5

EmilStenstrom opened this issue Jan 6, 2020 · 4 comments

Comments

@EmilStenstrom
Copy link

I'm running colorgram on images of board games that I have. Many of them have a black or white box, which means the dominant color in many of the cases is just black. I would like to filter out those colors.

I see that you already calculate luma here: https://github.com/obskyr/colorgram.py/blob/master/colorgram/colorgram.py#L65 -- so what I'm asking for is way to filter out those with high or low luma.

Is this something that is a good fit for this library?

@DonaldTsang
Copy link

Maybe remove background color (dominant color) first?

@EmilStenstrom
Copy link
Author

@DonaldTsang That would mean implementing something like this library myself... I would like help from this library for doing this, that is what this issue is about.

@sjmelchor
Copy link

sjmelchor commented Jul 27, 2023

@EmilStenstrom, I'm guessing you figured this out by now, but I found your comment while trying to solve the same issue. Here's what I ended up doing:

#running the colorgram tool as expected, I just set 10 as an arbitrary number of colors to extract

colors = colorgram.extract("your-favorite-jpeg", 10)

# on a scale from 0 to 255, anything with the lightness above 200 wouldn't work for me--but you could do the same in reverse for too dark.

def def too_light(trial_color):
  
      if trial_color.hsl[2] > 200:
          return True

#(colors.hsl[2], because lightness is the 3rd value in the hsl tuple)

#then later in my code, I made a list of the rgb tuples from the colors colorgram extracted, but only the colors that the function returned as False (ie, not too bright) made it into the list I used. 

I hope this helps someone! I'm new to GitHub and Python, so massive apologies if I didn't do it right. :/

@EmilStenstrom
Copy link
Author

@sjmelchor I did, but thanks for adding more context, I love finding these nuggets when I look for solutions elsewhere. Here's my solution which depends on the color's "luma" which is a calculated value which accounts for how light different colors feel:

image = Image.open(io.BytesIO(image_data)).convert('RGBA')
try_colors = 10
colors = colorgram.extract(image, try_colors)
for i in range(min(try_colors, len(colors))):
    color_r, color_g, color_b = colors[i].rgb.r, colors[i].rgb.g, colors[i].rgb.b

    # Don't return very light or dark colors
    luma = (
        0.2126 * color_r / 255.0 +
        0.7152 * color_g / 255.0 +
        0.0722 * color_b / 255.0
    )
    if (
        luma > 0.2 and  # Not too dark
        luma < 0.8     # Not too light
    ):
        break
   else:
        # As a fallback, use the first color
        color_r, color_g, color_b = colors[0].rgb.r, colors[0].rgb.g, colors[0].rgb.b

    return f"{color_r}, {color_g}, {color_b}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants