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
Answer from BaiJiFeiLong on Stack OverflowJust 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.
I am in the process of learning OOP and tuples. I am using the colorgram.py package to extract RGB color data from a .jpg.
import colorgram
# extract() method takes the arguments of a .jpg and the number of colors to extract
colors = colorgram.extract('hirst_image.jpg', 2)
print(f"A {colors}")
firstColor = colors[0]
rgb = firstColor.rgb
print(f"B {rgb}")
r = rgb[0]
g = rgb[1]
b = rgb[2]
print(f"C {r, g, b}")My confusion starts with what method colorgram.extract('hirst_image.jpg', 2) returns. print(f"A {colors}") displays
A [<colorgram.py Color: Rgb(r=250, g=249, b=245), 98.03692680376092%>, <colorgram.py Color: Rgb(r=207, g=165, b=127), 1.963073196239071%>]
When I see this I understand it as a list of dictionaries with a key of "Color" that has a value of list which is a tuple Rgb(r=250, g=249, b=245) and a float 98.03692680376092%. I feel like I'm incorrect in my understanding.
The second confusing bit is when I create the variable rgb = firstColor.rgb. When I print(f"B {rgb}") it is displayed as Rgb(r=250, g=249, b=245). As I understand this, I have pulled the tuple from the "Color" dict but I did it in a manner that doesn't make sense to me. Shouldn't I have extracted the tuple Rgb(r=250, g=249, b=245) with key of "Color" instead of firstColor.rgb?
In the end I got the result I was after, which was to pull out individual items from a tuple, I just don't understand how. This is not ok with me.
So, basically it's more a PyCharm problem than a Python one. I want to use the module 'colorgram' as I mentioned in the title, so I installed it with the package installer of PyCharm. But when I tried to call it with:
"import colorgram"
The program stopped running because of 'ModuleNotFoundError'. I tries using the same method for other modules, but no problem at all. Anybody else got into similar problems?