Tensorflow import error when loading PIL (pillow) - python

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

Related

DLL load failed: The specified module could not be found

I have installed Python 3.6.4. When I import cv2, it throws this error:
DLL load failed: The specified module could not be found.
I have OpenCV version 2.4.13.
What should i do?
It's possible that you have Python 2 and 3 installed and Python 3 doesn't know about your opencv installation. Try this:
pip3 install opencv-python
That will install opencv for Python 3 and hopefully that fixes your problem.
Try doing this:
Go to command prompt and "cd" the directory of python.
Enter this if you are running Python 3.x.x
pip3 install opencv-python
OR
2.If on Python 2.x.x
pip2 install opencv-python
DO NOT USE:
pip install opencv
That was my mistake and had to dig a lot to find a solution.
In my situation,
ImportError Traceback (most recent call last)
in ()
10 import os.path
11 from keras.models import load_model
---> 12 import cv2
13 from utils.utils import get_yolo_boxes, makedirs
14 from PIL import Image
ImportError: DLL load failed: The specified module could not be found.
Face this ImportError in the pass week, and I solved with enter below scripts in the Anaconda Prompt.
conda install -c menpo opencv
And here is my environment:
Python 3.6.5 Anaconda
Keras 2.2.2
tensorflow 1.9.0
For more details, here is the log I tried to solved this ImportError:
micky619/holiday-similarity#3
This can happen if you are using windows 10 N distribution, the N distributions does not come pre installed with windows media feature pack, which is required after OpenCV version 3.4 and onwards.
The preferred solution is to install the feature pack at : https://www.microsoft.com/en-us/software-download/mediafeaturepack
Be careful to choose the version that works with your current version of windows.
If that is not an option, fall back to an earlier version of OpenCV that does not have dll dependencies, you can do that by:pip install opencv-python=3.3.0.9
If the problem still persists try using Dependency walker to find out where specifically your problems stem from and then try fixing them individually.
Since windows rolled out it's N version this problem has been seen at many places, and has many impacts across the windows environment, the fastest way to identify if you have this problem is open youtube in Edge browser, if it says HTML5 media plugin not found, this is the problem.

PIL installation on anaconda for windows 10, 64 bit

when I ran conda-forge to get xgboost running, it seems to have affected few running packages, one of them being PIL. It was running properly until I ran conda-forge, but now I am getting the following error.
ImportError Traceback (most recent call last)
<ipython-input-3-588a23339ee5> in <module>()
5 import matplotlib.pyplot as plt
6 import scipy
----> 7 from PIL import Image
8 #from pillow import Image
9 from scipy import ndimage
C:\Anaconda3\lib\site-packages\PIL\Image.py in <module>()
54 # Also note that Image.core is not a publicly documented interface,
55 # and should be considered private and subject to change.
---> 56 from . import _imaging as core
57 if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
58 raise ImportError("The _imaging extension was built for another "
ImportError: DLL load failed: The specified module could not be found.
when I try to install PIL again using conda install pil, I get the following error message
(C:\Anaconda3) C:\Users\P Srinivasa Rao>conda install pil
Fetching package metadata ...............
Solving package specifications: .
UnsatisfiableError: The following specifications were found to be in conflict:
- pil -> python 2.6*
- python 3.6*
Use "conda info <package>" to see the dependencies for each package.
I then installed pillow, which seems to have gone well. But when I run from PIL import image, I still get the same error as above. I tried from pillow import image, it says no module found named pillow
Any advice on how to resolve this issue and get PIL working again?
I am using anaconda3 on windows 10, 64bit.
The problem looks like you have both pythons 2.x versions and a 3.x version. try the following
create a conda environment with python 3.6
conda create -n pil-env python=3.6
load PIL
conda install Pillow==4.0.0
I found Pillow 4.0.0 to be the most stable while using it on 3.6

I am using Pillow and getting an error "ImportError :No module named Image" in Ubuntu?

Does anyone know why I am getting this error?
I have Ubuntu 32-Bit , and Python 32-Bit and Installed Pillow using pip
Here is the code:
import Image
import pytesseract
print pytesseract.image_to_string(Image.open('1.jpg'))
Here is the error :
Traceback (most recent call last):
File "/home/siamak/workspace/test/com/tower/test/ocr.py", line 1, in
import Image
ImportError: No module named Image
You have to do:
from PIL import Image
Image is a submodule of PIL, so you have to use the from...import syntax.
I tested it on Linux Mint , and CentOS and it works perfectly .
For some reason it doesn't work on Ubuntu 32 and 64 .
I am assuming it is related to the PIL installation
Thanks
I also had this problem where,
from PIL import Image
gave this same error as the one in your comment:
from PIL import _imaging as core
ImportError: /home/jer/.local/lib/python2.7/site-packages/PIL/_imaging.so:
undefined symbol: PyUnicodeUCS4_AsLatin1String
It seems a locally installed version of Pillow is the culprit. We can uninstall the local version with,
pip uninstall Pillow
This left the system version of Pillow installed, which worked fine.

opencv 2.4.8 is not importing

As i am doing background subtraction on a video, opencv 2.4.6 is not supporting, like web cam will not respond.
my guide suggested me to download and install opencv 2.4.8, and i did so, but it is giving me an error
ImportError
<ipython-input-1-72fbbcfe2587> in <module>()
----> 1 import cv2
ImportError: numpy.core.multiarray failed to import
i have uninstalled the earlier version and then i have installed the new version, and done all the prerequisites.
Thank you for the support!
OpenCV relies on numpy, do you have numpy installed?

scipy.misc module has no attribute imread?

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)

Categories

Resources