Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError('Could not import PIL.Image. ' working with keras-ternsorflow

I'm following some lectures from lynda.com about deep learning using Keras-TensorFlow in a PyCharmCE enviroment and they didn't have this problem. I get this error:

raise ImportError('Could not import PIL.Image. ' ImportError: Could not import PIL.Image. The use of array_to_img requires PIL.

I have checked if others get the same error, but for me installing pillow using pip with the command pip install Pillow doesn't solve anything.

MacBook-Pro-de-Rogelio:~ Rogelio$ pip install Pillow Requirement already satisfied: Pillow in ./anaconda3/lib/python3.6/site-packages MacBook-Pro-de-Rogelio:~ Rogelio$

Any solution?

like image 845
Rogelio Em Avatar asked Jan 12 '18 11:01

Rogelio Em


4 Answers

All you need to do is install pillow:

pip install pillow

Then you should be all set. Found this after hours of searching.

like image 91
Wappler Avatar answered Nov 16 '22 08:11

Wappler


I had the exact same error and I fixed it the following way:

1) Run this command in your Jupyter Notebook:

import sys
from PIL import Image
sys.modules['Image'] = Image 

2) Run the following two lines in your notebook to be sure that they are correctly pointing to the same directory (if not it's because your PIL old library is messing up with the Pillow library)

from PIL import Image
print(Image.__file__)

import Image
print(Image.__file__)

3) If that's working correctly and both import prints pointing to the same python3 directory then move on. If not: 3.a) Go to your OS console and to your conda environment (be sure you are working within your desire conda environment) :

conda uninstall PIL
conda uninstall Pillow
conda install Pillow

You should now have successfully installed all the libraries for Pillow and let behind any problems with PIL. 3.b) Now try to execute the code of your jupyer notebook again, now the paths to both the imports should look exactly the same

4) Now, in the OS console/terminal, having your desired conda environment active, run the following commands:

conda install keras
conda install tensorflow

5) Run your jupyter notebook script again, It should be fixed and working now!

If it's still not working, it must be because you have opened a jupyter notebook kernel that's not point to the right environment. Fix that and you will be fine!

like image 26
Hernán Borré Avatar answered Nov 16 '22 09:11

Hernán Borré


If this problem is being seen on an Anaconda env, use

conda install pillow 

and reopen

like image 22
Sarath M Avatar answered Nov 16 '22 09:11

Sarath M


I ran into a similar issue with keras + tensorflow + miniconda.

I followed this advice from this issue : https://github.com/asataniAIR/Image_DL_Tutorial/issues/4 and did a pip install in conda admin console. So I enter

pip install --upgrade tensorflow keras numpy pandas sklearn pillow

on anaconda prompt, and add from sklearn.preprocessing import LabelEncoder in python code instead from PIL import Image

like image 4
Shaswat Rungta Avatar answered Nov 16 '22 09:11

Shaswat Rungta