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

Pass PIL Image Objects to ColorThief and Ignore White/Black Pixels #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

gsornsen
Copy link

Thanks for your work on Color Thief! It's a really excellent tool.

I'm working on a Linux project to sync the dominant color of my primary display to my Hue Lights. Unfortunately, Hue Sync only works on Windows/Mac.

I'd rather not be I/O bound having to read/write to a disc to get the dominant color and would like to use your tool as a dependency. This pull request reflects the necessary changes to make that happen.

Here is a code snippet using the modified ColorThief class:

#!/usr/bin/env python3

import pyscreenshot as ImageGrab
from displays import Monitor # Local import
from PIL import Image
from colorthief import ColorThief

# Get screen information from primary display
monitor = Monitor()
# Use pyscreenshot to grab a Linux screenshot using a native library
screen_grab = ImageGrab.grab(bbox=(monitor.x, monitor.y, monitor.width, monitor.height))
# Convert the image to a compatible PIL Image object
img = Image.frombytes('RGB', screen_grab.size, screen_grab.tobytes())
# Feed the PIL Image object to ColorThief
color_thief = ColorThief(img, True)
dominant_color = color_thief.get_color(quality=10000)
print(dominant_color)

@gsornsen gsornsen changed the title Pass PIL Image Objects to ColorThief Pass PIL Image Objects to ColorThief and Ignore White/Black Pixels Sep 21, 2019
@14ROVI
Copy link

14ROVI commented Oct 11, 2020

Hello,
I also started with this problem and i agree that it would be nice to be able to use Pillow images directly with it. However, a work around that works with the current:

with io.BytesIO() as file_object:
    pillow_image.save(file_object, "PNG")
    cf = ColorThief(file_object)
    colours = cf.get_color()

As you can see, this is quite elegant in its own sense too and it doesn't write to disc.

Hope that helps you!

just realised that this is from 2019 lmao

@ShubhamKhandare
Copy link

Hi,
I have directly passed screenshot to ColorThief and its seems to be working fine.

# Use pyscreenshot to grab a Linux screenshot using a native library
screen_grab = ImageGrab.grab(bbox=(monitor.x, monitor.y, monitor.width, monitor.height))
# Feed the PIL Image object to ColorThief
color_thief = ColorThief(screen_grab, True)

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

Successfully merging this pull request may close these issues.

3 participants