I have been reading images using (Python) skimage.imread
(or imageio.imread
) for months successfully, but now, without changing the code, I get failures when reading grayscale images. My collaborators can read the files. The image properties are:
identify test/resources/biosynth1_cropped/text_removed.png
test/resources/biosynth1_cropped/text_removed.png PNG 1512x315 1512x315+0+0 8-bit sRGB 48c 10094B 0.000u 0:00.005
and some properties with --verbose
Type: Grayscale
Base type: Undefined
Endianness: Undefined
Depth: 8-bit
Channel depth:
Red: 8-bit
Green: 8-bit
Blue: 8-bit
Channel statistics:
Pixels: 476280
The code (run in repl) is:
from skimage import io
import imageio
file="/Users/pm286/workspace/pyamiimage/test/resources/biosynth1_cropped/arrows_removed.png"
img = imageio.imread(file)
and gives errors:
(base) pm286macbook:pyamiimage pm286$ python test_load.py
Traceback (most recent call last):
File "test_load.py", line 9, in <module>
img = imageio.imread(file)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/functions.py", line 265, in imread
reader = read(uri, format, "i", **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/functions.py", line 186, in get_reader
return format.get_reader(request)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/format.py", line 170, in get_reader
return self.Reader(self, request)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/format.py", line 221, in __init__
self._open(**self.request.kwargs.copy())
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 298, in _open
return PillowFormat.Reader._open(self, pilmode=pilmode, as_gray=as_gray)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 138, in _open
as_gray=as_gray, is_gray=_palette_is_grayscale(self._im)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 689, in _palette_is_grayscale
palette = np.asarray(pil_image.getpalette()).reshape((256, 3))
ValueError: cannot reshape array of size 144 into shape (256,3)
I have (un/re)installed skimage
, imageio
, and pillow
.
(The code seems to read coloured images satisfactorily.)
I'd be grateful for pointers to solutions.
EDIT.The comment from @Nick ODell seems to have identified the problem. Has the dimensionality of the output changed from (x, y)to (x, y, 3)?
This is a bug which was fixed in imageio version 2.16.2. The issue is caused by Pillow returning image palettes with less than 256 colors.
To fix it, upgrade to imageio version 2.16.2 or later.
More information: