2

So I want to use imageio to read a black-and-white image as a 3d image. If I had used opencv I would use the following command since opencv's default is IMREAD_COLOR: im3d = cv2.imread(im_path) This results in shape (186, 148, 3)

However, since it is a black-and-white image, imageio contrary to opencv defaults to reading two dimensions. Thus im3d = imageio.imread(im_path) results in shape (186, 148).

According to the imageio docs, I need to add "format", a string with "The format to use to read the file." to make imageio behave the way I want to. See this link: https://imageio.readthedocs.io/en/stable/userapi.html#imageio.imread

But this is so badly explained! I am often amazed by how bad documentation is. I have no way to know what to enter as "format" in order to get imageio to behave as I want. So any help on this would be very appreciated!

HansHirse
  • 18,010
  • 10
  • 38
  • 67
Henrik Leijon
  • 1,277
  • 2
  • 10
  • 15

1 Answers1

1

Following the documentation link you provided, you can see a navigation item Docs for the formats quite prominent on the left side. For example, let's check the PNG format entry, since PNG supports single channel images. There's a section Parameters for reading: You need to provide a proper Pillow format string, for RGB images, it's 'RGB'. The parameter to be used is called pilmode. Let's check that:

import imageio

im = imageio.imread('path/to/some/singlechannelimage.png')
print(im.shape)

im3d = imageio.imread('path/to/some/singlechannelimage.png', pilmode='RGB')
print(im3d.shape)

If I run that for some single channel PNG, I get:

(639, 379)
(639, 379, 3)

That's what you want, I suppose.

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.8.5
imageio:       2.9.0
----------------------------------------
HansHirse
  • 18,010
  • 10
  • 38
  • 67