Reputation: 33
I have a grayscale image as 2d numpy array. I wanted to convert it into RGB image as 3d numpy array.
The RGB color was produced randomly.
import numpy as np
data = np.random.randint(low=0, high=255, size=(25, 25))
uniq_data = np.unique(data)
print (uniq_data)
colors = np.random.randint(low=0, high=255, size=(len(uniq_data), 3))
result = ?
I could not figure out how to do it.
I need to put unique color (RGB value) for unique data. The output shape would be (25, 25, 3).
Upvotes: 0
Views: 2207
Reputation: 53029
Make a lookup table and use it by means of advanced indexing:
lookup = np.zeros((255,3),dtype=np.uint8)
lookup[uniq_data] = colors
rgb = lookup[data]
Upvotes: 1