I am trying to create a system to take an array of floats which range from 0.0 to 1.0 and convert them into RGBA values based on a lookup table. The output should be an array that is one dimension larger that the input with the last dimension being size 4 and consisting of the RGBA values.
Currently I have only been able to do this via loops. Dose anyone know of any numpy indexing methods that could achieve this same result more efficiently.
import numpy as np
import matplotlib.pyplot as plt
cyan = np.array([(x*0,x*1,x*1,255) for x in range(256)])
input_array = np.arange(0,0.8,0.05).reshape(4,4)
input_array = input_array*256
colour_array = []
for x in range(input_array.shape[0]):
for y in range(input_array.shape[1]):
colour_array.append(cyan[int(input_array[x,y])])
colour_array = np.array(colour_array).reshape(4,4,4)
plt.imshow(colour_array)
Use the following:
shape = input_array.shape
index = input_array[*np.indices(shape).reshape(2, -1)].astype(int)
colour_array1 = cyan[index].reshape(4, *shape)
Confirm the two are equal:
np.allclose(colour_array, colour_array1,atol=0)
Out[62]: True
USE THE OTHER SOLUTION!!!