1. Error


ImportError: cannot import name ‘toimage’ from ‘scipy.misc’

  I was learning image processing in DL lecture. I had to convert float32 matrices into images. In the lecture, the code below is proposed.

  matrices[i].shape is 32, 32, 3, and ‘matrices’ save float32 value. The last dimension whose shape is 3 means the channel of images is RGB. But the code above did not work.



2. Reason


  scipy.misc.toimag is removed since version 1.2. The documentation suggests using PIL.Image.fromarray.

toimage is deprecated! toimage is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use Pillow’s Image.fromarray directly instead.



3. Solutions


1) Downgrade scipy module


  The first solution is downgrading scipy module. Uninstall first, if you already installed that.


2) Use PIL.Image


  This solution is following the suggetion of the documentation of Scipy.

  1. First, install pillow module, or install image module directly. or,

  2. Convert float32 values to 0-255 int8 format. Then, you can use Image.fromarray with the default mode.

      There are many modes of Image.fromarray. Check the documentation of pillow, and convert your array to an appropriate type.


3) Just convert type to 0-255 int type


  Because I used matplotlib.imshow, I could show the image using int matrix without converting that to an image.



Refers


SciPy.org (documentation)
Docs of Pillow
stack overflow, “How to create image from numpy float32 array?”
stack overflow, “ImportError with scipy.misc cannot import toimage”
ROBOT공학자, “[Python] scipy에서 imread가 안읽혀!!! ( ImportError: cannot import name ‘imread’ )”