dylanaraps / pywal

🎨 Generate and change color-schemes on the fly.
MIT License
8.23k stars 313 forks source link

Is there a way to provide a PIL image as input ? #591

Closed 0xARROWK closed 3 years ago

0xARROWK commented 3 years ago

Hi !

I write a script to stream the screen and detect the most present colour. However the colour detected as the most present is not very accurate, so I thought of using pywal which does the job much better while being very fast.

However, I retrieve images from PIL and I saw that pywal only accepts input paths. I don't know python that well, do you know if there is a way to provide the image to the module without saving the image to disk?

Here is an example of what I want to do :

from mss import mss
from pywal import colors as wal

from PIL import Image

# stream desktop screen and process with color
def start():

    with mss() as sct:

        swidth = 1920
        sheight = 1080

        resize_base = 150

        ratio = swidth / sheight
        resize_ratio = int(resize_base // ratio)

        mon = sct.monitors[0]

        i = 0
        for i in range(100):

            # grab image
            img = sct.grab(mon)

            # calcul current dominant color
            img = Image.frombytes("RGB", img.size, img.bgra, "raw", "RGBX")
            img = img.resize((resize_base, resize_ratio))
            # I want to use my img here 
            wal.get()
collidedscope commented 3 years ago

Color Thief‒one of the backends supported by pywal‒appears to make it very easy to go from a PIL.Image to its dominant color. In my limited testing, the palettes it generates aren't quite as nice as the ones from the Haishoku backend, but it seems to be the only one that doesn't require a path to a file in order to do its work.

0xARROWK commented 3 years ago

Okay, thank you for your answer ! Finally it seems that saving the image does not take too much time and does not reduce performance (for this I use the JPEG compression format, which is much faster than the PNG format).