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.

  • 2
    Possible duplicate of [scipy.misc module has no attribute imread?](https://stackoverflow.com/questions/15345790/scipy-misc-module-has-no-attribute-imread) – FlyingTeller Feb 22 '18 at 08:57

12 Answers12

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

FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
8

Use:

from imageio import imread

it worked for me.

Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
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

Chuk Ultima
  • 987
  • 1
  • 11
  • 21
  • 2
    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 – FlyingTeller Feb 22 '18 at 08:59
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
neouyghur
  • 1,577
  • 16
  • 31
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

shruthi_kr
  • 81
  • 1
  • 5
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
Sushanth
  • 2,224
  • 13
  • 29
3

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

pip install scipy==1.2.1
Beeti Sushruth
  • 321
  • 2
  • 12
0

Install PIL - Python imaging library. pip install Pillow

Manu mathew
  • 859
  • 8
  • 25
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)

-1

This works in the latest version...

from scipy.ndimage import imread
-1

This will work in latest version of scipy

from scipy.misc.pilutil import imread
-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