-
Notifications
You must be signed in to change notification settings - Fork 50
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
Comments
Maybe remove background color (dominant color) first? |
@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. |
@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:
I hope this helps someone! I'm new to GitHub and Python, so massive apologies if I didn't do it right. :/ |
@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}" |
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?
The text was updated successfully, but these errors were encountered: