Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing image to python :cannot import name 'imread'

Tags:

python

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.

like image 586
mohammad hassan bigdeli shamlo Avatar asked Feb 22 '18 08:02

mohammad hassan bigdeli shamlo


4 Answers

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

like image 110
FlyingTeller Avatar answered Nov 15 '22 13:11

FlyingTeller


Use:

from imageio import imread

it worked for me.

like image 40
2 revs, 2 users 60%user11449974 Avatar answered Nov 15 '22 12:11

2 revs, 2 users 60%user11449974


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

like image 4
Chuk Ultima Avatar answered Nov 15 '22 13:11

Chuk Ultima


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
like image 4
neouyghur Avatar answered Nov 15 '22 12:11

neouyghur