scipy.misc module has no attribute imread?
Asked Answered
P

18

181

I am trying to read an image with scipy. However it does not accept the scipy.misc.imread part. What could be the cause of this?

>>> import scipy
>>> scipy.misc
<module 'scipy.misc' from 'C:\Python27\lib\site-packages\scipy\misc\__init__.pyc'>
>>> scipy.misc.imread('test.tif')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    scipy.misc.imread('test.tif')
AttributeError: 'module' object has no attribute 'imread'
Perilymph answered 11/3, 2013 at 18:24 Comment(11)
which version of scipy are you using? scipy.__version__ gives 0.9.0 for me and i cannot reproduce your problemBath
do you get the same error if you do from scipy.misc import imread, and then imread('test.tif') ?Zerelda
@Zerelda yes, I get the same error for that.Perilymph
I can reproduce the problem on scipy version 0.10.1.Exanthema
I am using scipy version 0.11.0Perilymph
I think this function depends on PIL (pythonware.com/products/pil) being installed. Do you have PIL?Exanthema
No I don't. I will install it and give it a try.Perilymph
I installed pip install scipy==1.0.0 which worksEgression
imread was deprecated in SciPy 1.0.0, and is removed in 1.2.0. Use imageio.imread instead.Tooth
@Tooth Bhardwaj, Is imageio a replacement for scipy's misc? Because, in scipy.misc there was imresize functionality, which is not there in imageio. Can someone help me with this? Thank you.Harangue
Apparently, I found that imageio is not a replacement for the scipy.misc. Alternative for imresize of scipy.misc can be found in PIL library. refer this link for imresize alternativeHarangue
E
146

You need to install Pillow (formerly PIL). From the docs on scipy.misc:

Note that Pillow is not a dependency of SciPy but the image manipulation functions indicated in the list below are not available without it:

...

imread

...

After installing Pillow, I was able to access imread as follows:

In [1]: import scipy.misc

In [2]: scipy.misc.imread
Out[2]: <function scipy.misc.pilutil.imread>
Exanthema answered 11/3, 2013 at 18:34 Comment(8)
Orz, but if I install PIL, then it will give me <PngImagePlugin.PngImageFile...Materfamilias
It should be Pillow instead of PIL now. Reference: pillow.readthedocs.orgHousecoat
You saved my day-but why scipy documentation is so poor! My steps are install numpy+MKL>Pillow>Scipy all are downloaded from university of California at Irvine compiled python modules for windows.Statampere
READ THE NEXT ANSWER (@Shadab's answer) as well, and note that it's just imageio, not scipy.imageio.Macnamara
Note that this does not work with the latest version of SciPy (1.3.0). The solution from Shadab works.Skier
using the imageio module worked for me. Python 3.9.2 SciPy 1.6.1Kamalakamaria
Outdated answer for any recent SciPy version (>= 1.2).Accusative
i cant seem to get Pillow to work atm anyhow, no matter how i install it it says there is no module Pillow, and which pillow: pillow not found despite it also saying when i try to reinstall: Requirement already satisfied: Pillow in /usr/local/lib/python3.9/site-packages (8.3.1). so maybe thats a non standard path problem or a brew problem, but im not sure how to fix it. and the imageio solution just works. ive had Pillow installed and functional before in the past, so not sure whatsup.Disclaim
W
174

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

import imageio
im = imageio.imread('astronaut.png')
im.shape  # im is a numpy array
(512, 512, 3)
imageio.imwrite('imageio:astronaut-gray.jpg', im[:, :, 0])
Wisteria answered 18/12, 2017 at 9:43 Comment(8)
SciPy's imread used to return a numpy.ndarray. imageio.imread returns imageio.core.util.Array. If you want/need a numpy.ndarray and don't want to convert it, use matplotlib.pyplot.imread, as it also returns a numpy.ndarray.Entellus
I have a similar problem here, but with respect to imresize. Previously, scipy.misc.imresize works. Now it is deprecated and where will I get an alternative for this?Harangue
It's a joke @HarangueDucktail
@filip, I tried using the scipy.misc.imresize. It doesn't work now, but I had used it sometime back. I wish You could correct me if what I said had any mistakes in that comment rather than saying it as a joke. I am open to learning from my mistakes. What is wrong in my comment? Thank you.Harangue
Well I have to admit it I was a little bit frustrated with my comment at that time I wrote it :P. I meant that it's a joke they didn't provide wheels or installations below 1.0 for scipy on windows. If you're still looking to use it just use scipy==1.2.0 since it's still available there (just marked as deprecated, still works!) @HarangueDucktail
Does the imageio.core.util.Array have the correct dtype? Does a 8 bit image get returned at uint8?Shaikh
When writing your own code, use the newer alternatives. But when trying to run some code you know little about, it's easier to install an old scipy version, e.g. in conda conda install scipy==1.2.1. There seems to be no drop-in replacement for both imread and imresize.Youngling
@Entellus There is virtually no difference between a imageio.core.util.Array (it is - in fact - a ndarray subclass) and np.ndarray except for the additional _meta field. If you don't need metadata, you can simply call np.asarray(iio.imread(...)) to discard it; the "conversion" will be very efficient since there is nothing to convert. Also note that mpl forwards everything to pillow internally, whereas imageio may use additional backends if pillow support for a format isn't great (TIFF) or absent (raw formats, video, ...).Bronwen
E
146

You need to install Pillow (formerly PIL). From the docs on scipy.misc:

Note that Pillow is not a dependency of SciPy but the image manipulation functions indicated in the list below are not available without it:

...

imread

...

After installing Pillow, I was able to access imread as follows:

In [1]: import scipy.misc

In [2]: scipy.misc.imread
Out[2]: <function scipy.misc.pilutil.imread>
Exanthema answered 11/3, 2013 at 18:34 Comment(8)
Orz, but if I install PIL, then it will give me <PngImagePlugin.PngImageFile...Materfamilias
It should be Pillow instead of PIL now. Reference: pillow.readthedocs.orgHousecoat
You saved my day-but why scipy documentation is so poor! My steps are install numpy+MKL>Pillow>Scipy all are downloaded from university of California at Irvine compiled python modules for windows.Statampere
READ THE NEXT ANSWER (@Shadab's answer) as well, and note that it's just imageio, not scipy.imageio.Macnamara
Note that this does not work with the latest version of SciPy (1.3.0). The solution from Shadab works.Skier
using the imageio module worked for me. Python 3.9.2 SciPy 1.6.1Kamalakamaria
Outdated answer for any recent SciPy version (>= 1.2).Accusative
i cant seem to get Pillow to work atm anyhow, no matter how i install it it says there is no module Pillow, and which pillow: pillow not found despite it also saying when i try to reinstall: Requirement already satisfied: Pillow in /usr/local/lib/python3.9/site-packages (8.3.1). so maybe thats a non standard path problem or a brew problem, but im not sure how to fix it. and the imageio solution just works. ive had Pillow installed and functional before in the past, so not sure whatsup.Disclaim
A
62

imread is depreciated after version 1.2.0! So to solve this issue I had to install version 1.1.0.

pip install scipy==1.1.0
Amberlyamberoid answered 24/5, 2019 at 9:5 Comment(3)
See @Shadab's answer, it's in imageio now.Macnamara
It is removed starting 1.3.0, so the latest to install is at least 1.2.1Youngling
Removing and installing an older version is not a solution.Accusative
M
43

For Python 3, it is best to use imread in matplotlib.pyplot:

from matplotlib.pyplot import imread
Mishear answered 31/3, 2018 at 10:47 Comment(1)
No, its not. MatPlotlib converts to float. cv2.imread() correctly preserves the dtype of the image.Shaikh
E
19

In case anyone encountering the same issue, please uninstall scipy and install scipy==1.1.0

$ pip uninstall scipy

$ pip install scipy==1.1.0
Examination answered 26/6, 2019 at 5:8 Comment(1)
as @Pieter Meiresone removing and installing an older version is not a solution.It is may be conflict with other package that have dependency on scipy.Popper
B
11

As answered misc.imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. imageio is one option,it will return object of type :

<class 'imageio.core.util.Image'>

but instead of imageio, use cv2

import cv2
im = cv2.imread('astronaut.png')

im will be of type : <class 'numpy.ndarray'>

As numpy arrays are faster to compute.

Bandoline answered 20/5, 2018 at 12:36 Comment(1)
imageio works, but cv2 cause error : libpng warning: Invalid image height in IHDREclecticism
S
9

You need the Python Imaging Library (PIL) but alas! the PIL project seems to have been abandoned. In particular, it hasn't been ported to Python 3. So if you want PIL functionality in Python 3, you'll do well do use Pillow, which is the semi-official fork of PIL and appears to be actively developed. Actually, if you need a modern PIL implementation at all I'd recommend Pillow. It's as simple as pip install pillow. As it uses the same namespace as PIL it's essentially a drop-in replacement.

How "semi-official" is this fork? you may ask. The About page of the Pillow docs say this:

As more time passes since the last PIL release, the likelihood of a new PIL release decreases. However, we’ve yet to hear an official “PIL is dead” announcement. So if you still want to support PIL, please report issues here first, then open corresponding Pillow tickets here.

Please provide a link to the first ticket so we can track the issue(s) upstream.

However, the most recent PIL release on the official PIL site is dated November 15, 2009. I think we can safely proclaim Pillow as the successor of PIL after (as of this writing) nearly eight years of no new releases. So even if you don't need Python 3 support, I suggest you eschew the ancient PIL 1.1.6 distribution available in PyPI and just install fresh, up-to-date, compatible Pillow.

Salomie answered 11/4, 2017 at 0:53 Comment(0)
I
9

Install the Pillow library by following commands:

pip install pillow

Note, the selected answer has been outdated. See the docs of SciPy

Note that Pillow (https://python-pillow.org/) is not a dependency of SciPy, but the image manipulation functions indicated in the list below are not available without it.

Indium answered 24/9, 2017 at 14:29 Comment(0)
I
4

Imread uses PIL library, if the library is installed use :

from scipy.ndimage import imread

Source: http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.ndimage.imread.html

Induplicate answered 24/3, 2016 at 4:5 Comment(1)
this works for me ! though I don't understand why my professor use "from scipy.misc import imread" and it doesn't work on my laptopArmoury
M
3
python -m pip install pillow

This worked for me.

Marashio answered 17/9, 2017 at 23:0 Comment(0)
D
1

You need a python image library (PIL), but now PIL only is not enough, you'd better install Pillow. This works well.

Duplicature answered 6/8, 2016 at 11:35 Comment(1)
There's a comment on the accepted answer already stating that pillow should be used instead of PIL. I don't think it's a bad idea to add it as an answer, but it would be by far more useful if you explained why. Thanks.Polygyny
S
1

Running the following in a Jupyter Notebook, I had a similar error message:

from skimage import data
photo_data = misc.imread('C:/Users/ers.jpg')
type(photo_data)

'error' msg:

D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning: imread is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead. This is separate from the ipykernel package so we can avoid doing imports until

And using the following I got it solved:

import matplotlib.pyplot
photo_data = matplotlib.pyplot.imread('C:/Users/ers.jpg')
type(photo_data)
Shaefer answered 31/8, 2018 at 10:5 Comment(1)
It doesn't related to the question. It is a different problem and answer.Kepner
M
1

I have all the packages required for the image extraction on jupyter notebook, but even then it shows me the same error.

Error on Jupyter Notebook

Reading the above comments, I have installed the required packages. Please do tell if I have missed some packages.

pip3 freeze | grep -i -E "pillow|scipy|scikit-image"
Pillow==5.4.1
scikit-image==0.14.2

scipy==1.2.1
Meraree answered 25/3, 2019 at 13:23 Comment(0)
O
1

The solution that work for me in python 3.6 is the following

py -m pip install Pillow

Oldham answered 17/5, 2019 at 5:9 Comment(0)
S
1

The only way I could get the .png file I'm working with in as uint8 was with OpenCv.

cv2.imread(file) actually returned numpy.ndarray with dtype=uint8

Shaikh answered 4/9, 2020 at 23:11 Comment(0)
M
0

You must first install the Python version compatible with scipy (<3.7). I could not use pip to install scipy version 1.0 [ I think this version is no longer supported on pip] and used conda instead:

conda install -c anaconda scipy==1.0

Then to use "imread" you need to install Pillow.

pip install pillow
Morez answered 7/8, 2021 at 20:24 Comment(0)
B
0

imread is deprecated in scipy.misc; use imageio.imread instead.

imageio provides the same functionality as Scipy. But keep in mind that some arguments need to be changed (for detailed information please check here):

  1. Instead of mode, use the pilmode keyword argument.
  2. Instead of flatten, use the as_gray keyword argument.
Blindworm answered 4/10, 2021 at 18:49 Comment(0)
B
0

One way is to use PIL like this:

    from PIL import Image
    input_image = Image.open(filename)
Bircher answered 12/1, 2022 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.