99designs / colorific

Automatic color palette detection
ISC License
700 stars 55 forks source link

Colorific on Windows in a Python script #28

Open frakman1 opened 8 years ago

frakman1 commented 8 years ago

Hi there,

I am trying to use colorific in a python script running on Windows so I won't be running a binary from the command line or virtual environment as in your examples.

I was able to install it using python setup.py install

My question is how do I use it to operate on an image object such as one returned by:

image = ImageGrab.grab() # take a screenshot

I am running this in a loop and constantly calculate the primary colours of the resultant screenshot image. How do I use the colorific library with the image object in this way?

Note that I could save the images as files and then read them with the script but since it is a real-time application, that would introduce far too much latency.I was hoping for a colorific interface that allows an input that is an image object instead of a file-name.

Thanks!

illagrenan commented 8 years ago

@frakman1 Take a look how colorific binary handles color extraction https://github.com/99designs/colorific/blob/master/colorific/script.py#L83.

Here is simple example:

from PIL import Image
from colorific.palette import extract_colors, rgb_to_hex

def get_palette():
    filename = "image.jpg"
    image_pil = Image.open(filename)

    palette = extract_colors(image_pil)
    """:type : colorific.palette.Palette"""

    print("Colors: ", ', '.join(rgb_to_hex(c.value) for c in palette.colors))

    if palette.bgcolor:
        print("Background: ", rgb_to_hex(palette.bgcolor.value))