image - scipy.misc 图像函数(例如 imread、imresize、imsave、imshow 等)上的 AttributeError、ImportError

标签 image scipy python-import importerror

在尝试导入或直接使用 scipy.misc 模块中包含的任何图像函数时,我遇到了两种错误。以下是 imread() 函数的两个错误示例:

>>> from scipy.misc import imread

ImportError: cannot import name 'imread' from 'scipy.misc'

>>> import scipy.misc                                                                                                                                                                
>>> scipy.misc.imread

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

我做错了什么?

最佳答案

你没有做错任何事。这是由于 removal of the image functions from the scipy.misc module从 SciPy 版本 1.2.0 开始。我不知道他们为什么认为这些功能已弃用并删除它们,但如果您想使用它们,您可以通过卸载当前版本并安装以前的 SciPy 版本来回滚到以前的 SciPy 版本:

pip uninstall scipy
pip install scipy==1.1.0

确保您也安装了 Pillow:

pip install Pillow

如果您不想使用旧版本的 SciPy,则需要更改代码。根据每个已弃用函数的官方文档,以下是 SciPy 的建议:

  • fromimage(im) -> np.asarray(im)
  • imfilter() -> 直接使用 Pillow 过滤功能。
  • imread() -> imageio.imread()
  • imsave() -> imageio.imwrite()
  • imresize() -> numpy.array(Image.fromarray(arr).resize())
  • imrotate -> skimage.transform.rotate()
  • imshow() -> matplotlib.pyplot.imshow()
  • toimage() -> Image.fromarray()

假设安装以下库:

pip install numpy Pillow scikit-image imageio matplotlib

并导入它们:

import numpy as np, Pillow, skimage, imageio, matplotlib

此外,我引用了我找到的两个来源,提到了 scipy.misc 图像 I/O 功能的弃用:

来自 scipy.github.io :

The following functions in scipy.misc are deprecated: bytescale, fromimage, imfilter, imread, imresize, imrotate, imsave, imshow and toimage. Most of those functions have unexpected behavior (like rescaling and type casting image data without the user asking for that). Other functions simply have better alternatives.

来自 imageio.readthedocs.io (尤其是imread):

Transitioning from Scipy’s imread

Scipy is deprecating their image I/O functionality.

This document is intended to help people coming from Scipy to adapt to Imageio’s imread function. We recommend reading the user api and checkout some examples to get a feel of imageio.

Imageio makes use of variety of plugins to support reading images (and volumes/movies) from many different formats. Fortunately, Pillow is the main plugin for common images, which is the same library as used by Scipy’s imread. Note that Imageio automatically selects a plugin based on the image to read (unless a format is explicitly specified), but uses Pillow where possible.

In short terms: For images previously read by Scipy’s imread, imageio should generally use Pillow as well, and imageio provides the same functionality as Scipy in these cases. But keep in mind:

  • Instead of mode, use the pilmode keyword argument.
  • Instead of flatten, use the as_gray keyword argument.
  • The documentation for the above arguments is not on imread, but on the docs of the individual formats, e.g. PNG.
  • Imageio’s functions all return numpy arrays, albeit as a subclass (so that meta data can be attached).

关于image - scipy.misc 图像函数(例如 imread、imresize、imsave、imshow 等)上的 AttributeError、ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57796368/

相关文章:

python - 有效地将阈值函数应用于 SciPy 稀疏 csr_matrix

python - 在 Wing IDE 中安装 Pandas 导入错误

android - Android 上的 Worklight CSS 找不到图像文件夹

html - 我应该在 HTML 中为我的 IMG 指定高度和宽度属性吗?

python - 生成随机数以测试核密度估计

python - scipy.optimize.least_squares 是确定性的吗?

python - 如何使用这种结构在 Python 中进行相对导入?

python - Spacy 导入错误出 Undefined Symbol

javascript - Photoshop 脚本。打开和保存重叠图像堆栈

java - 如何有效地实现 java.awt.Composite?