将图像导入python:无法导入名称“imread”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48923151/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:54:26  来源:igfitidea点击:

Importing image to python :cannot import name 'imread'

python

提问by mohammad hassan bigdeli shamlo

I'm new to python and I want to import an image.

我是 python 的新手,我想导入一个图像。

import numpy as np
from scipy.misc import imread, imsave, imresize
# Read an JPEG image into a numpy array
img = imread('Cover.jpg')
print(img.dtype, img.shape)

but I face with following error: cannot import name 'imread'I've already successfully installed numpy and scipy.

但我面临以下错误:cannot import name 'imread'我已经成功安装了 numpy 和 scipy。

采纳答案by FlyingTeller

You also need to install PIL (Pillow) as that is what scipyuses to read images:

您还需要安装 PIL(枕头),因为它scipy用于读取图像:

pip install Pillow

note from the docs:

文档中的注释:

imread uses the Python Imaging Library (PIL) to read an image. The following notes are from the PIL documentation.

imread 使用 Python 图像库 (PIL) 来读取图像。以下注释来自 PIL 文档。

however, you might want to think about switching to scipy.imageio.imreadsince scipy.misc.imreadis deprecated:

但是,您可能需要考虑切换到scipy.imageio.imread因为scipy.misc.imread弃用

imread is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead

imread 已弃用!imread 在 SciPy 1.0.0 中已弃用,并将在 1.2.0 中删除。改用 imageio.imread

回答by Chuk Ultima

Apparently a lot of people had this issue and the solution was to install Pillow. Perhaps try to install Pillow and run it again

显然很多人都有这个问题,解决方案是安装Pillow. 也许尝试安装 Pillow 并再次运行它

sudo pip install Pillow==2.6.0

Source of information: https://github.com/Newmu/stylize/issues/1

信息来源:https: //github.com/Newmu/stylize/issues/1

回答by neouyghur

First, you should have Pillow, later your scipy version should be lower than 1.1.0

首先,你应该有 Pillow,之后你的 scipy 版本应该低于 1.1.0

pip install Pillow
pip install scipy==1.1.0

回答by Sushanth

Note: Posting the already given advises with a bit more as my reputation does not allow to comment

注意:由于我的声誉不允许发表评论,所以多发布一些已经给出的建议

In the latest version of scipy (1.3.0) functions like imread, imsave, imresize is deprecated. Downgrading scipy from 1.3.0 to 1.1.0 works like a charm and you will be able to use not just imread but all the above-mentioned functions which are almost necessary in most situations

在最新版本的 scipy (1.3.0) 中,不推荐使用 imread、imsave、imresize 等函数。将 scipy 从 1.3.0 降级到 1.1.0 就像一个魅力,您不仅可以使用 imread,还可以使用上述所有在大多数情况下几乎是必需的功能

The command for downgrading:

降级命令:

pip install scipy==1.1.0

回答by shruthi_kr

Install pillow

安装枕头

    pip3 install pillow

As scipy.misc is deprecated you cannot use it but instead

由于 scipy.misc 已弃用,您不能使用它而是

    from PIL import Image
    import numpy as np
    im = Image.open('hopper.jpg')
    a = np.asarray(im)
    im = Image.fromarray(a)

this returns an image object

这将返回一个图像对象

回答by Beeti Sushruth

From me this worked "from scipy.misc import imsave", when installed 1.2.1 version of scipy.

对我来说,当安装 1.2.1 版本的 scipy 时,这在“from scipy.misc import imsave”中起作用。

pip install scipy==1.2.1

回答by Yakshkumar Thakar

This will work in latest version of scipy

这将适用于最新版本的 scipy

from scipy.misc.pilutil import imread

回答by gaurav raj

Use:

用:

from imageio import imread

it worked for me.

它对我有用。

回答by Serhiy Yevtushenko

It could be that your version of scipy does not contain imread (https://docs.scipy.org/doc/scipy-1.2.1/reference/generated/scipy.misc.imread.html)

可能是您的 scipy 版本不包含 imread ( https://docs.scipy.org/doc/scipy-1.2.1/reference/generated/scipy.misc.imread.html)

Than use imageio.imread instead (see as well comments here on some changes in parameters names https://imageio.readthedocs.io/en/stable/scipy.html)

比使用 imageio.imread 代替(请参阅此处对参数名称的一些更改的评论https://imageio.readthedocs.io/en/stable/scipy.html

回答by Shakul Mittal

This works in the latest version...

这适用于最新版本...

from scipy.ndimage import imread