AttributeError while running Scipy.ndimages.imread - python

when running:
import scipy
scipy.ndimage.imread('path/to/image',mode='RGB')
I got
AttributeError: module 'scipy.ndimage' has no attribute 'imread'
I already tried to uninstall and reinstall scipy and also to reinstall Pillow and numpy as said there
Is there some missing module?

I came across this issue today as well. This happens because scipy.ndimage.imread is deprecated, see doc here.
To do the same, you can to use the imageio package
conda install -c conda-forge imageio
After this, you can do
import imageio
imageio.imread('path/to/image',mode='RGB')

A library that provides imread which most people already have is matplotlib. Just use it like this:
import matplotlib
matplotlib.pyplot.imread('path')

Using pytorch below method worked for me.
import matplotlib.pyplot as plt
plt.imread('Image_path')

Related

Numpy is installed, but astropy says numpy is not installed

An ImportError is thrown when my program searches for Numpy. Yet, numpy is installed as seen by the import numpy working properly.
I've tried
uninstalling python and numpy and reinstalling with choco
with pip
and with conda.
None seem to work. Any suggestions?
Any suggestions?
Does import numpy; print(numpy.__version__) work when you execute c:\agent03_work\s\turo.venv\bin\python.exe ? If not, you need to c:\agent03\_work\s\turo\.venv\bin\pip install numpy and you should be sorted.

ImportError: cannot import name 'imread' from 'scipy.misc' with tensorflow 2.2

i am using tensorflow 2.2 in virtualenv, i tried to import imread, imresize from from scipy.misc but got this error
from scipy.misc import imread, imresize
when i checked for the reason online, i figured out that scipy.misc is deprecated with the version of scipy required in tf2.2.
So i tried using imageio but getting error of PIL
don't know what to do, can anyone of you help?
the answer to my question is quite simple. I needed to install all the required packages of tensorflow2.2 mentioned in the documentation and use python 64bits

How can i fix the error for imports of own package? [duplicate]

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'
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])
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>
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
For Python 3, it is best to use imread in matplotlib.pyplot:
from matplotlib.pyplot import imread
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
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.
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.
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.
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
python -m pip install pillow
This worked for me.
You need a python image library (PIL), but now PIL only is not enough, you'd better install Pillow. This works well.
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)
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
The solution that work for me in python 3.6 is the following
py -m pip install Pillow
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
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
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):
Instead of mode, use the pilmode keyword argument.
Instead of flatten, use the as_gray keyword argument.
One way is to use PIL like this:
from PIL import Image
input_image = Image.open(filename)

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
From me this worked "from scipy.misc import imsave", when installed 1.2.1 version of scipy.
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)
This works in the latest version...
from scipy.ndimage import imread
This will work in latest version of scipy
from scipy.misc.pilutil import imread
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

Cannot import scipy.misc.imread

I've seen this problem before with other people, but haven't found a fix.
All I'm trying to do is:
from scipy.misc import imread
and I get
/home1/users/joe.borg/<ipython-input-2-f9d3d927b58f> in <module>()
----> 1 from scipy.misc import imread
/software/Python/272/lib/python2.7/site-packages/scipy/misc/__init__.py in <module>()
16 try:
17 from pilutil import *
---> 18 __all__ += pilutil.__all__
19 except ImportError:
20 pass
NameError: name 'pilutil' is not defined
But it's fine when I do from pilutil import * on its own (no import error). Even .../site-packages/scipy/misc/pilutil.py exists so I've got no idea why this is failing.
If you have Pillow installed with scipy and it is still giving you error then check your scipy version because it has been removed from scipy since 1.3.0rc1.
rather install scipy 1.1.0 by :
pip install scipy==1.1.0
check https://github.com/scipy/scipy/issues/6212
You might need to install PIL or Pillow.
The method imread in scipy.misc requires the forked package of PIL named Pillow. If you are having problem installing the right version of PIL try using imread in other packages:
from matplotlib.pyplot import imread
im = imread(image.png)
To read jpg images without PIL use:
import cv2 as cv
im = cv.imread(image.jpg)
looking into the documentation it says scipy.misc.imread is deprecated.
It says to install imageio, and to use imageio.imread instead.
Works great!
You can try
from scipy.misc.pilutil import imread instead of from scipy.misc import imread
Please check the GitHub page : https://github.com/amueller/mglearn/issues/2
for more details.
Expanding on user_3pij's answer
If you want to work with a scipy version that is higher than 1.3.0 then, as instructed in the scipy's documentation of the imread function, we can use the imageio module instead.
To successfully use the imageio imread function in a way that replicates the functionality of scipy's imread you can follow the instructions described here (disclaimer: I haven't tried it myself yet)
Scipy deprecated the image I/O functionality in v1.0 :
imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead
Using the imageio module:
img = imageio.imread(fina)
There may be some differences. See
https://imageio.readthedocs.io/en/stable/scipy.html
you can use this answer.
In newer versions of scipy, imread has been removed.
You can use imageio.imread instead.
import imageio
im = imageio.imread('example.png')
But if you want to use scipy, you have to use version 1.0 or 1.1.
To do this, use the following command.
conda install -c anaconda scipy==1.0
Then to use "imread" you need to install Pillow.you can use below command for install pillow :
pip install pillow
I received errors when trying to use
from scipy.misc import imread
I was able to remove the errors and use the above line by first installing numpy+mkl and then installing scipy from Christoph Gohlke's website.
For me this was:
pip install numpy-1.11.1+mkl-cp27-cp27m-win32.whl
pip install scipy-0.17.1-cp27-cp27m-win32.whl
You will need to pick the correct version of the whl's for your system.
Also, make sure the pip command installs the modules. If you have any 1 or more of these already installed, you might need to use pip to force a reinstall.
you have to import it like
from scipy import misc
it will work fine then.

Categories