Extract all colors from an image using colorgram
Question:
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?
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
You can work this after answers from given users but it won’t work because your format is "RGB". If you want to use that kind of color, you should convert it. For example:
import colorgram as cg
import turtle as t
colors = cg.extract('pink_image.jpg', 25)
rgb_colors = []
t.speed(100)
screen = t.Screen()
for coloring in colors:
r = coloring.rgb.r
g = coloring.rgb.g
b = coloring.rgb.b
new_color = (r, g, b)
rgb_colors.append(new_color)
print(rgb_colors)
Look output where it stays above after yours. When you look at your own code, you can see that kind of output:
>> [Rgb(r=245, g=243, b=238),Rgb(r=247, g=242, b=244)]
you can’t use this like that. You should convert it like my code. I hope that you understand it.
To get a usable list of rgb codes, you’ll need to use the code bellow:
import colorgram as cg
color_list = cg.extract("image.jpg", 10)
color_palette = []
for i in range(len(color_list)):
r = color_list[i].rgb.r
g = color_list[i].rgb.g
b = color_list[i].rgb.b
new_color = (r, g, b)
color_palette.append(new_color)
print(color_palette)
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?
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
You can work this after answers from given users but it won’t work because your format is "RGB". If you want to use that kind of color, you should convert it. For example:
import colorgram as cg
import turtle as t
colors = cg.extract('pink_image.jpg', 25)
rgb_colors = []
t.speed(100)
screen = t.Screen()
for coloring in colors:
r = coloring.rgb.r
g = coloring.rgb.g
b = coloring.rgb.b
new_color = (r, g, b)
rgb_colors.append(new_color)
print(rgb_colors)
Look output where it stays above after yours. When you look at your own code, you can see that kind of output:
>> [Rgb(r=245, g=243, b=238),Rgb(r=247, g=242, b=244)]
you can’t use this like that. You should convert it like my code. I hope that you understand it.
To get a usable list of rgb codes, you’ll need to use the code bellow:
import colorgram as cg
color_list = cg.extract("image.jpg", 10)
color_palette = []
for i in range(len(color_list)):
r = color_list[i].rgb.r
g = color_list[i].rgb.g
b = color_list[i].rgb.b
new_color = (r, g, b)
color_palette.append(new_color)
print(color_palette)