OSError in Pillow when calling Image.open() - python

For reference, I am using the Anaconda distribution of Python 3.4 on Windows x64, which includes Pillow automatically. I attempted to import Pillow, and it said that the package wasn't installed. I then used conda install Pillow to install Pillow. Now I have a different error.
I'm trying to use Pillow to open an image so that I can import it into a numpy array later. My code, simplified, is as follows.
import numpy as np
from PIL import Image
image = Image.open('foo.tif')
imageArray = np.array(image)
The error I get in the iPython console is as follows.
OSError: cannot identify image file 'foo.tif'
I've run conda list to see if I ran into the same problem as this SO question. However, I get this snippet in the full list:
patsy 0.2.1 np18py34_0
pillow 2.8.1 py34_0
pip 1.5.6 py34_0
This means that within my Anaconda 3.4 Python installation, there is no conflict between PIL and Pillow.
How do I best go about getting rid of this error?

Issue was in the file. Check integrity of your file before blaming anything else! Used a test tiff from here and it worked fine.

Related

Could not import PILLOW_VERSION from PIL

While importing, Python (Anaconda) gives the following error:
ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'
I tried removing pillow and then conda install but the error persists.
Pillow 7.0.0 removed PILLOW_VERSION, you should use __version__ in your own code instead.
https://pillow.readthedocs.io/en/stable/deprecations.html#pillow-version-constant
Edit (2020-01-16):
If using torchvision, this has been fixed in v0.5.0. To fix:
Require torchvision>=0.5.0
If Pillow was temporarily pinned, remove the pin
Old info (2020-01-09):
If using torchvision, there is a release planned this week (week 2, 2020) to fix it:
https://github.com/pytorch/vision/issues/1712#issuecomment-570286349
The options are:
wait for the new torchvision release
use the master version of torchvision (eg. pip install -U git+https://github.com/pytorch/vision)
install torchvision from a nightly, which also requires a pytorch from a nightly version
or install Pillow<7 (eg. pip install "pillow<7")
Downgrade pillow if you don't need latest features
pip uninstall pillow
pip install "pillow<7"
Or for anaconda,
conda install -c anaconda "pillow<7"
I have solved by modifying functional.py and __init__.py which are mentioned in error message.Error.
Modify from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION to from PIL import Image, ImageOps, ImageEnhance, __version__ in functional.py approx line number 5.
Modify PILLOW_VERSION = __version__ = _version.__version__ to __version__ = __version__ = _version.__version__ in __init__.py, approx line no 22.
File path:
functional.py:C:\Users\UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\torchvision\transforms\functional.py
__init__.py:C:\Users\UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\PIL\__init__.py
Currently using torchvision==0.4.2 and this solved my problem.
Downgrade your pillow to 6.1 and restart your Jupyter notebook.
Use this
conda install pillow=6.1
I found one another good solution:
Install Pillow-SIMD instead of Pillow.
Pillow-SIMD is "following" Pillow. Pillow-SIMD versions are 100%
compatible drop-in replacements for Pillow of the same version.
SIMD stands for "single instruction, multiple data" and its essence is
in performing the same operation on multiple data points
simultaneously by using multiple processing elements.
Homepage:
https://github.com/uploadcare/pillow-simd
Benchmarks:
https://python-pillow.org/pillow-perf/
Install instructions:
$ pip uninstall pillow
$ CC="cc -mavx2" pip install -U --force-reinstall pillow-simd
I checked: it has not such error and works on Pytorch/Torchvision.
You don't need to downgrade packages or change source code.
This is my fix to the error
Error: Could not import PILLOW_VERSION from PIL
OS: Linux 18.0 (LUBUNTU)
Python: 3.6
Resolved the issue by downgrading pillow:
pillow: 7.0.0-py36hb39fc2d_0 --> 6.1.0-py36h34e0f95_0
command: conda install pillow=6.1
The less painful way that worked for me is using alias
from PIL import __version__ as PILLOW_VERSION
Why?
Pillow is forked from PIL 1.1.7
VERSION was removed in Pillow 6.0.0
PILLOW_VERSION was removed in Pillow 9.0.0
Use __version__ instead
When you use some older packages, they try to import PILLOW_VERSION which is no longer available. So you'll need to use alias.

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)

Why can't I import opencv3 even though the package is installed?

I currently am running Python 3.5 and using Spyder from Anaconda as my IDE. I am running this on a Windows machine.
When I write import cv3 at the top of my code, it returns the error ImportError: No module named 'cv3'
I attempted to install opencv3 again with the command conda install -c https://conda.binstar.org/menpo opencv3 in the Command Prompt. It is apparently already installed because it returned
Fetching package metabase...............
Solving package specifications: .
# All requested packages already installed.
# packages in environment at C:\Users\Joey\Anaconda3:
# opencv3 3.1.0 py35_0 https://conda.binstar.org/menpo
Am I importing cv3 wrong? How do I fix this error?
Update: Tried import cv3 instead of import cv2 but got the following error: ImportError: cannot import name 'cv2'. The wording on the two errors is different, so python must acknowledge there is opencv installed but it does not work for some reason. Any ideas?
Ironically enough, the module is still called cv2 because it doesn't represent the version of opencv but the actual C++ API underneath which is, to be contrasted from the C API, named - cv2... So try with: import cv2
Problem solved by using command pip uninstall opencv-python in the Command Prompt.
I have attempted several installations of opencv and I suppose one may have downloaded badly and Anaconda was attempting to read that one. I looked into the build of some of the other installations I attempted and some were for Python 2.7. Maybe that contributed to the error.
Thankfully, this worked. Now import cv2 works perfectly. No errors.
I used the same approach to install the package. However, I could not import the library using the name opencv3. I had to use cv2 which worked for me.
Elaborating on #zwer's answer, check the version of OpenCV after import cv2.
>>> cv2.__version__
'3.1.0'
So basically it's calling the OpenCV3 library.

Anaconda and package installation (pydicom)

I have the following problem.
I have tried to install the pydicom package in python 2.7 using the following command (windows, anaconda setup):
conda install -c conda-forge pydicom
everything seems to work fine, the package seems to be installed.
I type
conda list
and in the list I see
pydicom 0.9.8 <pip>
I open spyder, or pycharm, type
import pydicom
and I get
ImportError: No module named pydicom
I have no idea what am I doing wrong. I went through http://conda.pydata.org/docs/using/pkgs.html and everything seems to be fine.
Please assist.
Since you are using 0.9.8, you actually need import dicom rather than import pydicom.
Due to this confusion, it will be import pydicom in version 1.0.0 and later.
I suggest you either update Python Version > 3.0, so that the output for conda list shows something like this:
(pip install pydicom)
pydicom 1.0.2 <pip>
python 3.6.4 h6538335_1
Now import using:
import pydicom #Preferable
==========================================================
Or install dicom instead of pydicom using:
(pip install pydicom-0.9.8)
pydicom 0.9.8 <pip>
python 2.7.0 h6538335_1
And then, import using:
import dicom
However, I strongly suggest that you install pydicom instead of dicom, since it is the upgraded version.

How to find and uninstall the numpy duplicate versions

I am trying to install the library GPy. Although the installation is successful, I have a question on my numpy version.
GPy library can be found here https://github.com/SheffieldML/GPy
The current version of my numpy is 1.9.3
>>> import numpy
>>> numpy.version.version
'1.9.3'
But when I perform python setup.py install for GPy, it refers to numpy 1.10.0. I checked in python 2.7/site-packages there only one version of numpy exist that too 1.9.3
Using /home/vinod/anaconda/lib/python2.7/site-packages
Searching for scipy==0.16.0
Best match: scipy 0.16.0
Adding scipy 0.16.0 to easy-install.pth file
Using /home/vinod/anaconda/lib/python2.7/site-packages
Searching for numpy==1.10.0
Best match: numpy 1.10.0
Adding numpy 1.10.0 to easy-install.pth file
Using /home/vinod/anaconda/lib/python2.7/site-packages
Finished processing dependencies for GPy==0.8.8
vinod#vinod-Lenovo-G580:~/GPy$
Since it's referring to another version am getting error like
File"__init__.pxd", line 155, in init GPy.util.linalg_cython (GPy/util/linalg_cython.c:4238)
ValueError: numpy.dtype has the wrong size, try recompiling
Could anyone tell me how to find and remove the numpy 1.10.0 ?
From the conda FAQ:
conda update numpy --no-pin
I tried the following steps and it works but still I don't know how.
I opened the setup.py and changed the numpy condition from numpy >= 1.7 to numpy <=1.9.3
Then I performed the python setup.py install
Then I uninstalled GPy using pip uninstall GPy
Again I installed GPy but using pip install GPy. Note: in previous steps I used git and installed separately.
This time it upgraded my numpy to 1.10.0 during installation and got installed successfully
Finally now it works well.
python Packeges
installed
select the library you want to unistall
"delate" by selecting tenter image description herehe menu on the right

Categories