Python scipy.misc 模块没有属性 imread?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15345790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scipy.misc module has no attribute imread?
提问by ustroetz
I am trying to read an image with scipy. However it does not accept the scipy.misc.imreadpart. What could be the cause of this?
我正在尝试使用 scipy 读取图像。但是它不接受该scipy.misc.imread部分。这可能是什么原因?
>>> import scipy
>>> scipy.misc
<module 'scipy.misc' from 'C:\Python27\lib\site-packages\scipy\misc\__init__.pyc'>
>>> scipy.misc.imread('test.tif')
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
scipy.misc.imread('test.tif')
AttributeError: 'module' object has no attribute 'imread'
采纳答案by Wilduck
You need to install Pillow(formerly PIL). From the docson scipy.misc:
您需要安装Pillow(以前称为PIL)。从文档开始scipy.misc:
Note that Pillow is not a dependency of SciPy but the image manipulation functions indicated in the list below are not available without it:
...
imread...
请注意, Pillow 不是 SciPy 的依赖项,但没有它,下面列表中指示的图像处理功能将不可用:
...
imread...
After installing Pillow, I was able to access imreadas follows:
安装 Pillow 后,我可以访问imread如下:
In [1]: import scipy.misc
In [2]: scipy.misc.imread
Out[2]: <function scipy.misc.pilutil.imread>
回答by Dhananjay Mehta
Imread uses PIL library, if the library is installed use : "from scipy.ndimage import imread"
Imread 使用 PIL 库,如果安装了该库,请使用:“from scipy.ndimage import imread”
Source: http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.ndimage.imread.html
来源:http: //docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.ndimage.imread.html
回答by K.A.Pendragon
回答by Josh Hansen
You need the Python Imaging Library (PIL) but alas! the PIL project seems to have been abandoned. In particular, it hasn't been ported to Python 3. So if you want PIL functionality in Python 3, you'll do well do use Pillow, which is the semi-official fork of PIL and appears to be actively developed. Actually, if you need a modern PIL implementation at all I'd recommend Pillow. It's as simple as pip install pillow. As it uses the same namespace as PIL it's essentially a drop-in replacement.
您需要 Python Imaging Library (PIL) 但唉!PIL 项目似乎已被放弃。特别是,它尚未移植到 Python 3。因此,如果您想在 Python 3 中使用 PIL 功能,最好使用Pillow,它是 PIL 的半官方分支,并且似乎正在积极开发中。实际上,如果您完全需要现代 PIL 实现,我会推荐 Pillow。就这么简单pip install pillow。由于它使用与 PIL 相同的命名空间,因此它本质上是一种替代品。
How "semi-official" is this fork?you may ask. The Aboutpage of the Pillow docs say this:
这个分叉有多“半官方”?你可能会问。Pillow 文档的“关于”页面是这样说的:
As more time passes since the last PIL release, the likelihood of a new PIL release decreases. However, we've yet to hear an official “PIL is dead” announcement. So if you still want to support PIL, please report issues here first, then open corresponding Pillow tickets here.
Please provide a link to the first ticket so we can track the issue(s) upstream.
随着自上次 PIL 发布以来的时间越长,发布新 PIL 的可能性就越低。然而,我们还没有听到官方的“PIL 已死”的消息。所以如果你还想支持 PIL,请先在这里报告问题,然后在这里打开相应的 Pillow 票。
请提供指向第一张票的链接,以便我们可以跟踪上游问题。
However, the most recent PIL release on the official PIL siteis dated November 15, 2009. I think we can safely proclaim Pillow as the successor of PIL after (as of this writing) nearly eight years of no new releases. So even if you don't need Python 3 support, I suggest you eschew the ancient PIL 1.1.6 distribution available in PyPI and just install fresh, up-to-date, compatible Pillow.
然而,PIL官方网站上最新的 PIL 版本发布日期为 2009 年 11 月 15 日。我认为我们可以安全地宣布 Pillow 是 PIL 的继任者(在撰写本文时)近八年没有新版本发布。因此,即使您不需要 Python 3 支持,我建议您避开 PyPI 中提供的古老的 PIL 1.1.6 发行版,只需安装全新的、最新的、兼容的 Pillow。
回答by José Carlos Aquino
python -m pip install pillow
This worked for me.
这对我有用。
回答by Steve
Install the Pillow library by following commands:
通过以下命令安装 Pillow 库:
pip install pillow
Note, the selected answer has been outdated. See the docs of SciPy
请注意,所选答案已过时。查看SciPy的文档
Note that Pillow (https://python-pillow.org/) is not a dependency of SciPy, but the image manipulation functions indicated in the list below are not available without it.
请注意 Pillow ( https://python-pillow.org/) 不是 SciPy 的依赖项,但没有它,下面列表中指示的图像处理功能将不可用。
回答by Shadab
imreadis deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use imageio.imreadinstead.
imread在 SciPy 1.0.0 中已弃用,并将在 1.2.0 中删除。使用imageio.imread来代替。
import imageio
im = imageio.imread('astronaut.png')
im.shape # im is a numpy array
(512, 512, 3)
imageio.imwrite('imageio:astronaut-gray.jpg', im[:, :, 0])
回答by MasterJedi
For Python 3, it is best to use imreadin matplotlib.pyplot:
对于 Python 3,最好使用imreadin matplotlib.pyplot:
from matplotlib.pyplot import imread
回答by kshitij_p
As answered misc.imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. imageio is one option,it will return object of type :
正如回答 misc.imread 在 SciPy 1.0.0 中已弃用,并将在 1.2.0 中删除。imageio 是一种选择,它将返回类型的对象:
<class 'imageio.core.util.Image'>
but instead of imageio, use cv2
但不是 imageio,而是使用 cv2
import cv2
im = cv2.imread('astronaut.png')
im will be of type :
<class 'numpy.ndarray'>
我将是类型:
<class 'numpy.ndarray'>
As numpy arrays are faster to compute.
由于 numpy 数组的计算速度更快。
回答by Mike
Running the following in a Jupyter Notebook, I had a similar error message:
在 Jupyter Notebook 中运行以下命令,我收到了类似的错误消息:
from skimage import data
photo_data = misc.imread('C:/Users/ers.jpg')
type(photo_data)
'error' msg:
“错误”消息:
D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning:
imreadis deprecated!imreadis deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Useimageio.imreadinstead. This is separate from the ipykernel package so we can avoid doing imports until
D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning:
imread已弃用!imread在 SciPy 1.0.0 中已弃用,并将在 1.2.0 中删除。使用imageio.imread来代替。这与 ipykernel 包是分开的,因此我们可以避免进行导入,直到
And using the following I got it solved:
并使用以下我解决了它:
import matplotlib.pyplot
photo_data = matplotlib.pyplot.imread('C:/Users/ers.jpg')
type(photo_data)

