解决:AttributeError: module ‘scipy.misc’ has no attribute ‘imsave’


解决:AttributeError: module ‘scipy.misc’ has no attribute ‘imsave’



文章目录

  • 解决:AttributeError: module 'scipy.misc' has no attribute 'imsave'
    • 背景
    • 报错问题
    • 报错翻译
    • 报错位置代码
    • 报错原因
    • 解决方法
      • 方法一 scipy版本回退(不推荐)
      • 方法二 使用imageio保存图片即可(推荐)
      • 方法三 使用opencv保存图片(推荐)
      • 方法四 使用matplotlib保存图片(推荐)
      • 今天的分享就到此结束了



背景

在使用之前的代码时,报错:
Traceback (most recent call last):
File “xxx”, line xx, in
misc.imsave(output_path, scaled_temp)
AttributeError: module ‘scipy.misc’ has no attribute ‘imsave’



报错问题


Traceback (most recent call last):
  File "xxx", line xx, in 
    misc.imsave(output_path, scaled_temp)
AttributeError: module 'scipy.misc' has no attribute 'imsave'


报错翻译

主要报错信息内容翻译如下所示:


Traceback (most recent call last):
  File "xxx", line xx, in 
    misc.imsave(output_path, scaled_temp)
AttributeError: module 'scipy.misc' has no attribute 'imsave'

翻译:

追溯(最近一次通话):
文件“xxx”,第xx行,在中
misc.imsave(output_path,scaled_temp)
AttributeError:模块“scipy.misc”没有属性“imsave”



报错位置代码

from scipy import misc
misc.imsave(output_path, scaled_temp)


报错原因

经过查阅资料,发现这个错误产生的原因是scipy模块1.20版本之后该方法已经被弃用了,如果继续使用这个方法,就会报这样的错误。

小伙伴们按下面的解决方法即可解决!!!



解决方法

要解决这个错误,需要这里总结了以下几个解决办法:

方法一 scipy版本回退(不推荐)

回退到scipy模块1.20之前的版本,版本回退的pip指令如下:

pip install scipy==1.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

注意:因为版本回退后会造成该模块与环境中的tensorflow包或者其他包的不兼容,后续还得再更新回来,或者是单独建一个环境,比较麻烦。

方法二 使用imageio保存图片即可(推荐)

imageio的方法正确的代码是:

import imageio
imageio.imwrite(output_path,scaled_temp)

方法三 使用opencv保存图片(推荐)

opencv的方法:

import cv2 
# 读取
img = cv.imread(imagepath)
# 显示
cv2.imshow('window_title',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存
cv.imwrite(savepath, img)

方法四 使用matplotlib保存图片(推荐)

matplotlib的方法:

from PIL import Image
import matplotlib.pyplot as plt

plt.imshow(img)
plt.savefig(img_name+'.png')# 图像保存
plt.show()


今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

你可能感兴趣的:(#,Python,Bug,scipy,python)