Col
Col

Reputation: 33

Converting a 2d array (grayscale image) into 3d (rgb image) array

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

Answers (1)

Paul Panzer
Paul Panzer

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

Related Questions