在想把numpy 数据转为图像的时候需要用到imsave(),imresize, imsave 等问题但是在import scipy.misc 之后显示该库内没有imsave()
错误如下:
scipy.misc import toimage
File "D:/Agronne/Tensorflow/...../WGAN实战/gan_train.py", line 5, in <module>
from scipy.misc import toimage
ImportError: cannot import name 'toimage' from 'scipy.misc' (C:\conda\lib\site-packages\scipy\misc\__init__.py)
1. toimage() 无法导入的问题
查询了一堆博客和google,大部分说是应为pillow 未装好,我重装了最新版的pillow后还是不行,后来去了scipy官网,发现人家scipy 1.2.0后删除了toimage,改用pillow中的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.
Takes a numpy array and returns a PIL image.
This function is only available if Python Imaging Library (PIL) is installed.
The mode of the PIL image depends on the array shape and the pal and mode keywords.”
所以别再傻傻的装pillow了,使用PIL
from PIL import Image
Image.fromarray(final_image)
运行正确
2. imsave()等的问题
同样的,imread,和imsave 也在scipy中去除了,分别改用了imageio 中imread,imsave 替换了
官网介绍如下:
“imsave is deprecated! imsave is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imwrite instead.
imread, is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.”
import imageio
im = imageio.imread('astronaut.png')
im.shape # im is a numpy array
(512, 512, 3)
imageio.imwrite('imageio:astronaut-gray.jpg', im[:, :, 0])