Here are the basic steps to an app I'm writing in Python:
Here is my function, and I get the following error: TypeError: 'int' object has no attribute '_getitem_' on the line sorted_by_dist = sorted(colorMap, key=lambda tup: tup[0])
# Sorts a list of colors according to distance from the source color
def sort_colors(colors, source):
colorMap = ()
sortedColors = list()
for i in range(len(colors)):
dist = dist_3d(colors[i], source)
colorMap = colorMap + (dist, colors[i])
sorted_by_dist = sorted(colorMap, key=lambda tup: tup[0])
for (d, c) in sorted_by_dist:
sortedColors.append(c)
return sortedColors
Assuming my dist_3d() function is correct and returns an integer value (it is, and does), what am I doing wrong? I don't understand.
When organizing by color, look to the rainbow. Walker recommends this method as you sort: "Start with white, cream, pink, red, orange, yellow, green, blue, indigo, violet, brown, gray, and black," she says. Gold and silver can be tricky, so she suggests moving them around to see what looks best to you.
RGB sorting The most trivial way in which we can sort colours is by directing sort its RGB values. Python has a very naive way of doing this: it sorts the first component, then the second and finally the third one.
Colour sorters are used for the food processing industry, such as coffee, nuts, and oil crops. The goal is the separation of items that are discoloured, toxic (such as ergot), not as ripe as required, or still with hull after dehulling such as sunflower seeds..
You are building your colorMap
as a big single dimensional tuple, with the first index being an int
. So your lambda
is being passed an int
and then you try to index into it.
You probably want a list of tuples:
colorMap = []
...
dist = dist_3d(colors[i], source)
colorMap.append((dist, colors[i]))
In terms of an approach to sorting colors, I have actually used a kdtree
module for this, loaded up with all my RGB tuples. Then I can ask it for N closest colors to a given color tuple:
from kdtree import KDTree
colors = [(10,10,10),(250,50,5),(100,50,20)]
query_color = (175, 25, 50)
tree = KDTree.construct_from_data(data)
# query the 2 closest colors
nearest = tree.query(query_point=query_color, t=2)
The error is because tup
is an int
. __getitem__
is the name of the []
operator.
But, it looks like all your function is doing is sorting a list of colors, based on distance from some reference color. And, you already have dist_3d
implemented. Given that it works, then sorting your colors is much simpler. Sort them in place:
colors.sort(key=lambda color: dist_3d(color, reference_color))
or as a new list:
sorted_colors = sorted(unsorted_colors, key=lambda color: dist_3d(color, reference_color))
No need for all that tuple-building and looping and stuff in your function, as I can tell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With