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)
Related
I'm having an issue with my tensorflow on windows 10 (python 3.7, tf 2.1.0, keras 2.3.1, pillow 7.1.1). I had some code I wrote on an osx machine and was porting over to windows. I am using conda envs and have all the necessary packages installed (tensorflow, keras, pillow, opencv, etc) but I am getting the following error
Traceback (most recent call last):
...
"...\venv\lib\site-packages\keras_preprocessing\image\utils.py", line 108, in load_img
raise ImportError('Could not import PIL.Image. '
ImportError: Could not import PIL.Image. The use of `load_img` requires PIL.
I have tried reinstalling pillow, tensorflow, keras, creating a new environment, installing with pip and repeated this in about every location I can think of but this issue remains persistent. Any help would be greatly appreciated.
Thank you
One solution that might work for your case:
First, you need to : pip install pillow (uninstall + reinstall / upgrade it it's the case)
Second, you need to change your imports in this way:
from IPython.display import display
from PIL import Image
This solution can be found in the following thread:
ImportError: Could not import the Python Imaging Library (PIL) required to load image files on tensorflow
I am trying to open a JPG image using matplotlib in Python. Editor 'Spyder', Python3.6, WIndows 7
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Read in the image and print some stats
image = mpimg.imread(r'C:\Users\xxx\Python Code\mountain.jpg')
print('This image is: ',type(image),
'with dimensions:', image.shape)
But I am getting the following error... It says that except '.png' no other image format is supported.
Error :--
image = mpimg.imread(r'C:\Users\xxx\Python Code\mountain.jpg')
File "C:\temp\Continuum\anaconda3\lib\site-packages\matplotlib\image.py",
line 1284, in imread
'more images' % list(handlers))
ValueError: Only know how to handle extensions: ['png']; with Pillow
installed matplotlib can handle more images.
I went through various documentations. Which says that, in order to open a '.jpg' image, 'Pillow' must be installed.
If native matplotlib call fails to open a image then it automatically falls back on 'pillow'. (correct me if I am wrong)
So I installed 'Pillow'. But I am still getting the error.
Can you tell me what am I Missing ? (Strange thing is this same code is running in another computer. I have no way to verify what library is installed in that machine)
Matplotlib requires PIL(Python Imaging Library) to work with .jpg format. To use it you need to install Pillow (which is the fork of PIL).
Installation Using PIP
pip install pillow
or
pip3 install pillow
Installation Using Conda
conda install pillow
You have to install PIL. Make sure you are using anaconda python distro. Go to this link or write this command to install PIL directly
conda install -c anaconda pillow
I am trying to run the following simple code
import scipy
scipy.test()
But I am getting the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 586, in runfile
execfile(filename, namespace)
File "C:/Users/Mustafa/Documents/My Python Code/SpectralGraphAnalysis/main.py", line 8, in <module>
import scipy
File "C:\Python27\lib\site-packages\scipy\__init__.py", line 61, in <module>
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
ImportError: cannot import name NUMPY_MKL
I am using python 2.7 under windows 10.
I have installed scipy but that does not seem to solve the problem
Any help is appreciated.
If you look at the line which is causing the error, you'll see this:
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
This line comment states the dependency as numpy+mkl (numpy with Intel Math Kernel Library). This means that you've installed the numpy by pip, but the scipy was installed by precompiled archive, which expects numpy+mkl.
This problem can be easy solved by installation for numpy+mkl from whl file from here.
Reinstall numpy-1.11.0_XXX.whl (for your Python) from www.lfd.uci.edu/~gohlke/pythonlibs. This file has the same name and version if compare with the variant downloaded by me earlier 29.03.2016, but its size and content differ from old variant. After re-installation error disappeared.
Second option - return back to scipy 0.17.0 from 0.17.1
P.S. I use Windows 64-bit version of Python 3.5.1, so can't guarantee that numpy for Python 2.7 is already corrected.
I'm not sure if this is a good solution but it removed the error.
I commented out the line:
from numpy._distributor_init import NUMPY_MKL
and it worked. Not sure if this will cause other features to break though
I had the same problem while installing gensim on windows. Gensim is dependent on scipy and scipy on numpy. Making all three work is real pain. It took me a lot of time to make all there work on same time.
Solution:
If you are using windows make sure you install numpy+mkl instead of just numpy.
If you have already installed scipy and numpy, uninstall then using "pip uninstall scipy" and "pip uninstall numpy"
Then download numpy-1.13.1+mkl-cp34-cp34m-win32.whl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
and install using pip install numpy-1.13.1+mkl-cp34-cp34m-win32.wh
Note: in cp34-cp34m 34 represent the version of python you are using, so download the relevant version.
Now download scipy from http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy (appropriate version for your python and system)
and install using "pip install scipy‑0.19.1‑cp34‑cp34m‑win32.whl"
Your numpy and Scipy both should work now.
These binaries by Christoph Gohlke makes it very easy to install python packages on windows. But make sure you download all the dependent packages from there.
I don't have enough reputation to comment but I want to add, that the cp number of the .whl file stands for your python version.
cp35 -> Python 3.5.x
cp36 -> Python 3.6.x
cp37 -> Python 3.7.x
I think it's pretty obvious but still I wasted almost an hour because of this and maybe other people struggle with that, too.
So for me worked version cp36 that I downloaded here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
since I am using Python 3.6.8.
Then I uninstalled numpy:
pip uninstall numpy
Then I installed numpy+mkl:
pip install <destination of your .whl file>
The reason for the error is you upgraded your numpy library of which there are some functionalities from scipy that are required by the current version for it to run which may not be found in scipy. Just upgrade your scipy library using python -m pip install scipy --upgrade. I was facing the same error and this solution worked on my python 3.5.
From your log its clear that numpy package is missing. As mention in the PyPI package:
The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation.
So, try installing numpy package for python as you did with scipy.
I recently got the same error when trying to load scipy in jupyter (python3.x, win10), although just having upgraded to numpy-1.13.3+mkl through pip.
The solution was to simply upgrade the scipy package (from v0.19 to v1.0.0).
yes,Just reinstall numpy,it works.
I have installed (actually reinstalled) scipy:
10_x86_64.whl (19.8MB): 19.8MB downloaded
Installing collected packages: scipy
Successfully installed scipy
But the misc subpackage is apparently not included?
16:03:28/shared $ipython
In [1]: from scipy.misc import imread
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-f9d3d927b58f> in <module>()
----> 1 from scipy.misc import imread
ImportError: cannot import name imread
What is the way to install the scipy.misc package?
I think you need to install PIL as well. From the scipy.misc docs:
Note that the Python Imaging Library (PIL) is not a dependency of SciPy and therefore the pilutil module is not available on systems that don’t have PIL installed.
I had same problem, running Python 2.7.12 on an old Windows XP/SP3 box. I had some stuff running on Python on MacBook, and wanted to make it work on an old Windows box. It can be done. The winbox had pip ver. 8, and I upgraded it to pip ver. 9, from within Python, using the suggestion pip provides when you run it. I had installed numpy and Pillow (current ver of PIL), using "pip install numpy" and "pip install Pillow", but "pip install scipy" and "pip install scipy.misc" failed with "no matching distribution found". I had to uninstall numpy, and then install two files: 1) numpy+mkl and then 2) scipy, both files installed are binaries for Windows, in .whl (wheel) archive format, downloaded from: http://www.lfd.uci.edu/~gohlke/pythonlibs/
site maitained by Christoph Gohlke. Find the binary versions you need for your flavour of Windows, and downloaded them into C:\some\directory. Installation order is important. First the numpy+mkl is installed, using pip, with the scipy file second. I downloaded the files from Gohlke's site, and then used pip to install them. For my old winbox, this was:
C:\some\directory\> pip install numpy-1.12.1rc1+mkl-cp27-cp27m-win32.whl
(you should see)
Installing collected packages: numpy
Successfully installed numpy-1.12.1rc1+mkl
(then, you can run)
C:\some\directory\> pip install scipy-0.18.1-cp27-cp27m-win32.whl
and you should see the "Successfully installed..." message. I had already installed Pillow.
Confirm by starting Python, and trying:
>>> import numpy as np
>>> from PIL import Image, ImageDraw
>>> import scipy.misc
and all these should work. You should be able to render a .jpg with:
image = Image.open("Somefile.jpg")
image.show()
and your somefile.jpg will be displayed.
I've used Pillow in the past on OSX without problems, however I now get the following error.
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 61, in <module>
from PIL import _imaging as core
ImportError: dlopen(/Library/Python/2.7/site-packages/PIL/_imaging.so, 2): Library not loaded: /usr/local/lib/libjpeg.8.dylib
Referenced from: /Library/Python/2.7/site-packages/PIL/_imaging.so
Reason: image not found
I've seen this question which appears to be a similar problem but I don't think I installed Pillow with brew.
I've also tried the solution from this question but the command
pip install PIL --allow-external PIL --allow-unverified PIL
dosent seem to work and I get an error (no such option: --allow-unverified)
Lastly, I've tried to recreate the symbolic link to libjpeg.8.dylib but that also did not make any difference.
Would anyone know how to fix this error? Do I need to do something to relink _image.so aswell as libjpeg?
Python PIL was deprecated eons ago and you should not attempt to use it anymore.
What you want is python[23] -m pip install Pillow
PS. Using pip command will be deprecated soon because of confusions regarding python interpreters, instead of calling pip ... everyone should use only python -m pip ... which assures that is calling the right interpreter.