convert a grayscale image to a 3-channel image [duplicate]

Solution 1:

You can use np.stack to accomplish this much more concisely:

img = np.array([[1, 2], [3, 4]])
stacked_img = np.stack((img,)*3, axis=-1)
print(stacked_img)
 # array([[[1, 1, 1],
 #         [2, 2, 2]],
 #        [[3, 3, 3],
 #         [4, 4, 4]]])