Colorgram: removing Rgb from each element
Question:
I have this list:
in which I would like to remove all the characters and leave the actual values such that I get the following:
[(157, 155, 164),…,(56, 66, 70)]
I tried
s = [x.strip('Rgb(r=') for x in s]
but that didn’t work, neither did this:
s = s.replace("Rgb(r=", "(").replace("g=", "").replace("b=", "")
Thanks in advance!
Answers:
This is a list of Rgb
objects, not strings. You can likely do
[(rgb.r, rgb.g, rgb.b) for rgb in s]
to get the attributes of those objects.
Edit: After a little research, it looks like Rgb
is a namedtuple
. That means that they’re iterables, so we can just do
[tuple(rgb) for rgb in s]
though they’re already technically tuples.
import colorgram
colors = colorgram.extract("hirst painting.jpg", 10)
rgb_colors = []
for color in colors:
r = color.rgb.r
g = color.rgb.g
b = color.rgb.b
new_color = (r, g, b)
rgb_colors.append(new_color)
print(rgb_colors)
#[(253, 251, 248), (254, 250, 252), (232, 251, 242), (198, 12, 32), (250, 237, 17), (39, 76, 189), (38, 217, 68), (238, 227, 5), (229, 159, 46), (27, 40, 157)]
import colorgram
colors = colorgram.extract('image.jpg',10)
rgb = []
for color in colors:
r = color.rgb.r
g = color.rgb.g
b = color.rgb.b
new_color = (r,g,b)
rgb.append(new_color)
print(rgb)
I have this list:
in which I would like to remove all the characters and leave the actual values such that I get the following:
[(157, 155, 164),…,(56, 66, 70)]
I tried
s = [x.strip('Rgb(r=') for x in s]
but that didn’t work, neither did this:
s = s.replace("Rgb(r=", "(").replace("g=", "").replace("b=", "")
Thanks in advance!
This is a list of Rgb
objects, not strings. You can likely do
[(rgb.r, rgb.g, rgb.b) for rgb in s]
to get the attributes of those objects.
Edit: After a little research, it looks like Rgb
is a namedtuple
. That means that they’re iterables, so we can just do
[tuple(rgb) for rgb in s]
though they’re already technically tuples.
import colorgram
colors = colorgram.extract("hirst painting.jpg", 10)
rgb_colors = []
for color in colors:
r = color.rgb.r
g = color.rgb.g
b = color.rgb.b
new_color = (r, g, b)
rgb_colors.append(new_color)
print(rgb_colors)
#[(253, 251, 248), (254, 250, 252), (232, 251, 242), (198, 12, 32), (250, 237, 17), (39, 76, 189), (38, 217, 68), (238, 227, 5), (229, 159, 46), (27, 40, 157)]
import colorgram
colors = colorgram.extract('image.jpg',10)
rgb = []
for color in colors:
r = color.rgb.r
g = color.rgb.g
b = color.rgb.b
new_color = (r,g,b)
rgb.append(new_color)
print(rgb)