Search code examples
pythonimageimreadpython-imageio

Import image as RGB pixels of "imageio.core.util.Image" type in Python


I want to import some images with its split RGB values, For some images it works and for some others, the output gives just one value for RGB of a pixel.

Here's the image for which the code works:

if os.path.isfile(location1):  
    image = imageio.imread(location1)
print("Type : ", type(image[0][0]))
## Type : imageio.core.util.Image
input : image
output: Image([[[167, 126,  94],
        [210, 184, 147],
        [245, 234, 188],
        ...,

And this is the image for which the code doesn't work.

if os.path.isfile(location2):  
    image = imageio.imread(location2)
print("TYpe : ", type(image[0][0]))
## TYpe : <class 'numpy.uint8'>
input: image
output: Image([[81, 78, 74, ..., 72, 71, 69],
      [74, 71, 67, ..., 70, 70, 68],
      [61, 58, 55, ..., 65, 65, 64],
   ...,

(I would appreciate any help)


Solution

  • It seems to be the second image you loaded is simply a grayscale image (i.e. not an image with color, but only with gray levels). To convert it to RGB, try the following:

    from skimage import color
    img = color.gray2rgb(your_image)
    

    Also, as the conversion to RGB is just to repeat each gray value three times, you can use this snippet

    import numpy as np
    rgb = np.stack((your_image, your_image, your_image), axis=-1)