0

I am trying to get the shape of images in my custom data generator :

for j in range(len(image)):
    img = np.array(image[j])
    print(img.shape)
    plt.imshow(img)
    plt.show()

The img.shape shows (128, 87) without the third dimension, so I expect to see a grayscale image, while the plt.imshow(img) shows the full-color image.

On the other hand, I converted grayscale to RGB using the following code:

def grayscale_to_rgb(input_image):
   input_image = np.expand_dims(input_image, -1)
   input_image_3_channel =input_image.repeat(3, axis=-1)
   return input_image_3_channel

the image shape will be (128,87,3) but I see a grayscale image using plt.imshow(img).

I have already seen this question. What I understood is the grayscale image is still grayscale, but imshow shows the default cmap. So, how can I see the RGB image in color?

Zara Nz
  • 9
  • 5
  • The default [colormap](https://matplotlib.org/stable/users/prev_whats_new/dflt_style_changes.html#colormap) of matplotlib is `viridis`. When displaying an image with a single channel matplotlib applies colormap, and when displaying RGB there is no colormap. You may try [other packages](https://www.askpython.com/python/examples/display-images-using-python) for displaying the image. Or select gray colormap as described [here](https://stackoverflow.com/a/3823822/4926757). – Rotem Mar 23 '23 at 21:19
  • Your `grayscale_to_rgb()` function is producing an image in which the red, green, and blue components of each pixel are equal - the only pixel colors possible are shades of gray. If you want there to be any color in the result, you'd have to handle the three components unequally. – jasonharper Mar 23 '23 at 21:38
  • @jasonharper thanks for your reply. Can you provide the code for that ? – Zara Nz Mar 24 '23 at 16:25
  • Code for *what*, exactly? How do you want colors to be assigned to your image? – jasonharper Mar 24 '23 at 16:31
  • @jasonharper how can i handle the three components unequally if i want to see the rgb images in color ? – Zara Nz Mar 24 '23 at 18:28
  • You can do whatever you want - there's no inherent color in the original image, so there's no proper way to make up colors for it. For example, set the green and blue components to 0, to make the image red - or perhaps multiply them by 0.5, for a more subtle reddish tint. – jasonharper Mar 24 '23 at 18:35

0 Answers0