Extract all colors from an image using colorgram

Viewed 2992

I am willing to get all of the colors present in an image in python using colorgram. In the documentation they don't specifically say about this. Here is my code:

import colorgram as cg

color_list = cg.extract("dot-img.jpg", 60)
color_palette = []

for count in range(len(color_list)):
    rgb = color_list[count]
    color = rgb.rgb
    color_palette.append(color)

I wrote 60 in cg.extract second argument. But I don't get 60 colors. Does that mean that colorgram returned me all of the colors present in the image?

1 Answers

Just give the number_of_colors variable a number as big as possible. System will process it properly.

import colorgram as cg

color_list = cg.extract("dot-img.jpg", 2 ** 32)
color_palette = []

for count in range(len(color_list)):
    rgb = color_list[count]
    color = rgb.rgb
    color_palette.append(color)

The source code in colorgram:

def get_colors(samples, used, number_of_colors):
    pixels = 0
    colors = []
    number_of_colors = min(number_of_colors, len(used))

    for count, index in used[:number_of_colors]:
        pixels += count

        color = Color(
            samples[index]     // count,
            samples[index + 1] // count,
            samples[index + 2] // count,
            count
        )

        colors.append(color)
    for color in colors:
        color.proportion /= pixels
    return colors
Related