吴无需降级scipy的情况下解决AttributeError: module ‘scipy.misc‘ has no attribute ‘imread‘,imresize,imsave等问题

无需降级scipy的情况下解决以下问题:
AttributeError: module ‘scipy.misc’ has no attribute ‘imread’,
AttributeError: module ‘scipy.misc’ has no attribute ‘imresize’,
AttributeError:module ‘scipy.misc’ has no attribute 'imsave’

imread,imresize,imsave

最近遇到如下三个错误
AttributeError: module ‘scipy.misc’ has no attribute ‘imread’,
AttributeError: module ‘scipy.misc’ has no attribute ‘imresize’,
AttributeError:module ‘scipy.misc’ has no attribute ‘imsave’。
原因是scipy在新版本中misc库中弃用了一部函数,其中就包括imread,imresize和imsave。

在很多回答中都提到了降低scipy的版本,但是觉得这种解决方法很不爽,官方弃用这些函数肯定有他的道理,不能遇到问题就总想着降版本。

AttributeError: module ‘scipy.misc’ has no attribute 'imread’的解决方法

代码如下

from scipy import misc
img = misc.imread(image_path)

错误如下

Traceback (most recent call last):
  File "Make_aligndata_git.py", line 57, in <module>
    img = misc.imread(image_path)
AttributeError: module 'scipy.misc' has no attribute 'imread'

修改如下

import imageio
img = imageio.imread(image_path)

AttributeError: module ‘scipy.misc’ has no attribute 'imresize’的解决方法

代码如下

from scipy import misc
scaled_temp = misc.imresize(cropped_temp, (image_size, image_size), interp='bilinear')

错误如下

Traceback (most recent call last):
  File "Make_aligndata_git.py", line 98, in <module>
    scaled_temp = misc.imresize(cropped_temp, (image_size, image_size), interp='bilinear')
AttributeError: module 'scipy.misc' has no attribute 'imresize'

修改如下

from skimage.transform import resize
scaled_temp = resize(cropped_temp,output_shape=(image_size, image_size))

AttributeError: module ‘scipy.misc’ has no attribute 'imsave’的解决方法

代码如下

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

错误如下

Traceback (most recent call last):
  File "Make_aligndata_git.py", line 104, in <module>
    misc.imsave(output_filename, scaled_temp)
AttributeError: module 'scipy.misc' has no attribute 'imsave'

修改如下

import imageio
imageio.imwrite(output_filename,scaled_temp)

那么问题是scipy在新版本的misc库中为什么要弃用这些函数呢呢,我也不知道,
这个misc是miscellaneous缩写(杂项的意思)

Miscellaneous routines (scipy.misc)
Various utilities that don’t have another home.

意思就是不知道放哪的函数都放这里了。
在这里插入图片描述
我们可以看到很多和cv相关的函数都被弃用了
我猜大概可能是因为想要使用这些函数必须安装Python Imaging Library(PIL)库才可以,scipy的开发者觉得这不符合他们性格,可能就给弃用了…
以后直接使用imageio库下的函数就可以了
imageio的API

猜你喜欢

转载自blog.csdn.net/qq_42859149/article/details/119508200
今日推荐