14

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.

1

12 Answers 12

15

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

Sign up to request clarification or add additional context in comments.

Comments

8

Use:

from imageio import imread

it worked for me.

Comments

4

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

1 Comment

It's not a "workaround" since it is not a bug, it is expected behaviour, since scipy need PIL(Pillow) to be able to read images. It even says in the docs
4

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

Comments

3

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

Comments

3

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

Comments

3

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

pip install scipy==1.2.1

Comments

0

Install PIL - Python imaging library. pip install Pillow

Comments

0

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)

Comments

-1

This works in the latest version...

from scipy.ndimage import imread

Comments

-1

This will work in latest version of scipy

from scipy.misc.pilutil import imread

Comments

-2

Change to imageio will fix the problem

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.