AttributeError: module 'scipy.misc' has no attribute 'toimage'

在使用scip.misc.toimage保存文件的时候,报错说是scipy.misc没有toimage。

代码片段如下:

scipy.misc.toimage(img, cmin=0, cmax=1).save(name)

网上找了半天都是提示我pillow没有安装,然后通过pip、pip3、conda均做了尝试,还是报同样的错误,然后卸载重新安装scipy和pillow还是不行。然后我用的python3,查了python3的使用toimage,发现这个函数已经deprecated。

https://docs.scipy.org/doc/scipy-1.2.0/reference/generated/scipy.misc.toimage.html

这里说一下,用这个Image.fromarray的时候有个坑,这个函数对float支持的不好,即使把mode设置成'F'出来的图片失真也很严重,什么也看不出来。这里可以把(0-1)的float数据乘上255,然后转成Unit8(0~255)就可以了,然后把mode设置成'L'就是想要的图片了。

from PIL import Image 

Image.fromarray((image_array*255).astype('uint8'), mode='L').convert('RGB').save(filename)

python3相对于python对应包改动太大,很多语法及包引用的时候还是需要知道自己对照的版本,不然拿到的代码想跑起来需要大改了。

发布了83 篇原创文章 · 获赞 18 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/lwc5411117/article/details/100654865
今日推荐