The following script creates a RGB array from a colour-ramp contained inside a list. Elements of the array ‘cabbage’ are indices to the list ‘cucumber’. The following script creates an array ‘cauliflower’ with the same shape as ‘cabbage’ but with the indices replaced with the corresponding tuples from ‘cucumber’. Is there a more direct way in Numpy to carry out this procedure?
from numpy import array, shape, zeros
cabbage = array([[0,3,2],[3,2,1],[3,1,0]])
cucumber=[(0,100,0),(0,150,0),(0,200,0),(0,255,0)]
rows ,cols = shape(cabbage)
cauliflower = zeros((rows,cols),dtype=object)
for row in range(rows):
for col in range(cols):
cauliflower[row,col]=cucumber[cabbage[row,col]]
print cauliflower
[[(0, 100, 0) (0, 255, 0) (0, 200, 0)]
[(0, 255, 0) (0, 200, 0) (0, 150, 0)]
[(0, 255, 0) (0, 150, 0) (0, 100, 0)]]
Numpy supports fancy indexing: