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


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



文章目录

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



背景

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



报错问题


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


报错翻译

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


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

翻译:

追溯(最近一次通话):
文件“xxx”,第xx行,在中
杂项imread(输入路径)
AttributeError:模块“scipy.misc”没有属性“imread”



报错位置代码

from scipy import misc
misc.imread(input_path)


报错原因

经过查阅资料,发现这个错误产生的原因是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.imread(input_path)

方法三 使用opencv读取图片(推荐)

opencv的方法:

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

方法四 使用matplotlib读取图片(推荐)

matplotlib的方法:

import matplotlib.pyplot as plt# plt 用于显示图片
import matplotlib.image as mpimg# mpimg 用于读取图片
import numpy as np
  
lena= mpimg.imread('lena.png')# 读取和代码处于同一目录下的 lena.png
# 此时 lena 就已经是一个 np.array 了,可以对它进行任意处理
lena.shape#(512, 512, 3)
  
plt.imshow(lena)# 显示图片
plt.axis('off')# 不显示坐标轴
plt.show()


今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

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