简体   繁体   中英

Importing image to python :cannot import name 'imread'

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

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.

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

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.

however, you might want to think about switching to scipy.imageio.imread since scipy.misc.imread is deprecated :

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

Use:

from imageio import imread

it worked for me.

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

sudo pip install Pillow==2.6.0

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

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

pip install Pillow
pip install scipy==1.1.0

Install pillow

    pip3 install pillow

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

    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

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

The command for downgrading:

pip install scipy==1.1.0

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

pip install scipy==1.2.1

Install PIL - Python imaging library. pip install Pillow

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 )

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

这适用于最新版本...

from scipy.ndimage import imread

这将适用于最新版本的 scipy

from scipy.misc.pilutil import imread

更改为 imageio 将解决问题

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM