I'm trying to convert an RGB image to a greyscale image, then to a numpy array using the following code snippet:
img = Image.open("image1.png")
img = img.convert('L')
img = np.array(img, dtype='f')
print(img.shape)
The result is a numpy array of shape (128, 128). Is there anyway that I could convert a greyscale image to a numpy array so that it would have the color channel as well, i.e. the shape would be (128, 128, 1)?
img = img[..., np.newaxis]to get that shape but I don't see how that gives you colour.(128, 128)array are the "colors" here. Sometimes you need to have a singleton dimension, but it's usually because you are using numpy broadcasting or something similar. Adding a singleton dimension changes nothing about the underlying data. So you have a good answer now from @crazycoder, but I am not sure you have a solution to your actual problem.