image read through skimage.io.imread have suspicious shape

25.6k views Asked by At

I am trying to read an RGB image using the skimage.io.imread. But after reading the image, I found that the image shape is wrong, print(img.shape) shows that the image shape is (2,). The complete code to show the problem is:

from skimage import io
img = io.imread(path/to/the/image)
print(img.shape)

I also tried to read the image using opencv's python package, the returned shape is correct (height*width*3).

The skimage version used is 0.12.3, can someone explain is there anything wrong with my way using the package or is this really a bug?

Click the link for the test image

Edit1

The test image is altered when it is uploaded, the unaltered version is here. I have also opened an issue on the skimage github repo, and it turns out that the test image is a two-frame image, but the second frame is empty. You can consider this image a "corrupted" image.

In order to read the right image, you can use this workaround, img = io.imread(/path/to/the/image, img_num=0).

2

There are 2 answers

1
Tonechas On BEST ANSWER

You can fix this issue by enforcing skimage.io.imread() to use matplotlib:

In [131]: from skimage import io

In [132]: img = io.imread('156.jpg', plugin='matplotlib')

In [133]: img.shape
Out[133]: (978L, 2000L, 3L)

Your image is likely to be a multi object JPG. If you try to read it using PIL (which is the default plugin) you get a NumPy array which consists of two objects. The first object is the image itself and the second one might be a thumbnail, but PIL does not handle it properly:

In [157]: img = io.imread('156.jpg', plugin='pil')

In [158]: img.dtype
Out[158]: dtype('O')

In [159]: img.shape
Out[159]: (2L,)

In [160]: img[0].shape
Out[160]: (978L, 2000L, 3L)

In [161]: img[1]
Out[161]: array(<PIL.MpoImagePlugin.MpoImageFile image mode=RGB size=2000x978 at 0x111DBCF8>, dtype=object)

Take a look at this thread to find out more on this problem.

1
Jeru Luke On

Check the type of image being uploaded by you.

If you upload a color image you will get the size of the image along with the number of channels (1920, 2560, 3).

As long as the uploaded image is a color image you will receive 3.

Or else if the image is a gray-scale or binary you will get the size of the image (1920, 2560)