当前位置: 代码迷 >> 综合 >> AttributeError: module 'scipy.misc' has no attribute 'toimage'
  详细解决方案

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

热度:61   发布时间:2023-12-18 15:52:13.0

想将numpy矩阵保存成图片,出现如下错误,

Traceback (most recent call last):

  File ".\demo2.py", line 29, in <module>

    sm.toimage(image_array).save(filename)

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

 

原因:

1.2.0以上版本的scipy已经删除这个方法了。

解决方法:

方法1:

降低scipy版本,执行下面命令,

pip3 install scipy==1.2.1

 

方法2:

使用 Image.fromarray函数,

将原来的

sm.toimage(image_array).save(filename)

改成

Image.fromarray(image_array).convert('L').save(filename)

如果想保存成RGB图片,则将上面的 'L'  改为 'RGB' 即可。

 

  相关解决方案