Get colors from images python script

Get colors from image with this python script and example. Retrieve all colors from one or multiple images with this Python solution and easy Guide.

You can open Photoshop, or like me Pixelmator, but getting all the colors with the eyedropper tool can be a lot of work. So why not use a “get colors from images” python script.

Getting colors from image

When you design websites or want to create a style for a new company you are mostly to start with colors. Now when you want to work from a certain image you really like or a pre-made logo, you might want to have all the colors from that image or logo.

You can make use of an eyedropper that can be found in an image-editing software, like Photoshop, Lightroom or Pixelmator. But when a image has many colors, you might end up spending your entire day clicking to get out all the colors.

_An _eyedropper_ is a tool found in many image editing programs that let you click on a point in the image to identify and select its color._

RGB and HEX colors

Now getting out these colors might take you a long time, but getting the RGB and HEX value can also be a lot of work.

More and more both RGB and HEX are being used within CSS / SCSS / SASS and LESS for creating websites. And as I was creating this site, and the API Python version of it it took me quite some time to get all the colors out of the logo’s.

The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors.

Both HEX and RGB are used to define colors.

A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors.

Create color python script

Now I do not like repetitive work and clicking within an image to get out all the colors really looks a lot to me like repetitive work.

So I created this small color extracting script. It shows you per image all the different colors with hex and GRB values in a terminal.

"""
    author: Theo van der Sluijs
    url: https://itheo.tech
    copyright: CC BY-NC 4.0
    creation date: 20-12-2018

    This script gets all colors from an image
    and displays then in the terminal in rgb and hex values (with coloring!)

    Just place your image or images in a folder.
    Set the action for one or multiple (list) files
    And run the script.
    Sit back and relax

    You need Pillow and Colr for this
    https://python-pillow.org/
    https://pypi.org/project/Colr/

    Please install by :
    pip install Pillow
    pip install Colr
"""
    from PIL import Image
    from colr import Colr as C

    class getColors:
        def __init__(self, files=None):
            if files is None:
                return None

            if type(files) is str:
                self.proces_image(files)
            else:
                for f in files:
                    self.proces_image(f)

        def proces_image(self, image):
            colors = self.get_colors(image)
            seperator = '================================'
            print(C().b_rgb(0, 0, 0).rgb(255, 255, 255, seperator))
            print(C().b_rgb(0, 0, 0).rgb(255, 255, 255, image))
            print(C().b_rgb(0, 0, 0).rgb(255, 255, 255, seperator))
            for c in colors:
                self.show_colors(c[1])

        def get_colors(self, file):
            img = Image.open(file)
            return img.convert('RGB').getcolors()

        def show_colors(self, color):
            r = color[0]
            g = color[1]
            b = color[2]
            rgb = "rgb({}, {}, {}) | {}".format(r, g, b, self.rgb_to_hex(color))
            print(C().b_rgb(0, 0, 0).rgb(r, g, b, rgb))

        def rgb_to_hex(self, rgb):
            return '#%02x%02x%02x' % rgb


    if __name__ == '__main__':
        # one file
        file = "data/P-logo-final-PNG.png"
        # multiple files as list
        files = ['data/P-logo-final-PNG.png', 'data/P-logo-final-PNG2.png']

        g = getColors(files)

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!