77

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?

Null
  • 1,950
  • 9
  • 30
  • 33
Rogelio Em
  • 779
  • 1
  • 5
  • 4
  • Are you installing pillow in the same environment you installed the tensorflow instance you're using? With anaconda, people often teach you to install tensorflow and keras in a separate environment. You must install pillow in that same environment. – Daniel Möller Jan 12 '18 at 12:40
  • You may, if you confirm the first question, try to uninstall pillow and install it again. – Daniel Möller Jan 12 '18 at 12:40
  • A similar problem can be found here: https://stackoverflow.com/questions/41124353/importerror-could-not-import-the-python-imaging-library-pil-required-to-load – Eulenfuchswiesel Dec 19 '18 at 14:15

15 Answers15

141

All you need to do is install pillow:

pip install pillow

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

Lux
  • 17,835
  • 5
  • 43
  • 73
Wappler
  • 1,411
  • 1
  • 6
  • 2
  • 3
    that solved my problem too while attempting to follow [Deep Learning with Python, by Francois Chollet, Chapter 5.2.4](https://books.google.co.kr/books/about/Deep_Learning_with_Python.html?id=Yo3CAQAACAAJ&source=kp_book_description&redir_esc=y) – bit_scientist May 09 '19 at 06:35
  • 1
    Thank goodness I found this answer before I installed 'pil' instead of `pillow` -- my conda environment was just about to be absolutely decimated – eric Jun 10 '19 at 14:20
  • 4
    `conda install pillow` worked for me in my conda environment. Note I had to restart Spyder before things would work for me. – eric Jun 10 '19 at 14:32
  • 7
    If you are using jupyter notebook, you also have to restart the kernel to make it work. – Q. Qiao Sep 30 '19 at 19:08
  • 1
    @eric Restarting kernel would be enough from Consoles > Restart kernel – Canbey Bilgili Apr 20 '20 at 10:57
23

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!

Pdubbs
  • 1,967
  • 2
  • 11
  • 20
Hernán Borré
  • 391
  • 1
  • 5
23

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

conda install pillow 

and reopen

mistertandon
  • 1,424
  • 14
  • 15
Sarath M
  • 386
  • 2
  • 4
4

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

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
Shaswat Rungta
  • 3,635
  • 2
  • 19
  • 26
2

Here's what worked for me. Uninstall the conda version of pillow and install the pip version, then restart the kernel of your Jupyter Notebook

conda uninstall --force pillow


pip install pillow
dpacman
  • 3,683
  • 2
  • 20
  • 35
1

pip install pillow This did it for me as well.

I am using Jupyter Notebook, and Tensorflow2.0 Keras. To set the context, I got this error when i was trying to use builtin image.load_img() function in Keras. You will have to restart your kernel as well after doing this install.

coder_sqrl
  • 11
  • 1
1

I encountered the same problem while working on Pycharm. Even after trying various methods on the internet, I could not solve it. When I ran the code on Jupyter notebook, it asked me to install the module, SciPy. I installed it and the code is now working on Pycharm.

James Albert
  • 75
  • 2
  • 7
1

I have the same problem good news is it can be easily solvable. my problem was: I was using jupyter notebook for my python project which was launched using Anaconda navigator. In the python program I was using the function

image_ = image.load_img(image_path, target_size=(224,224))

then I got this error "raise ImportError('Could not import PIL.Image. ' ImportError: Could not import PIL.Image."

solution worked for me: in the environment you are using for jupyternotebook firstly uninstall pillow using the command

conda uninstall --force pillow then install it using the command pip install pillow (type these commands in the anaconda prompt) then close all your notebooks and anaconda navigator, open anaconda navigator with the environment that you have installed before, in that environment launch the jupyter notebook and run the code it will work

1MS19CS067
  • 11
  • 3
0

I had the exact same question. And I fixed it by changing my environment variables. Because I had two versions of python in my windows PC.

So I changed the priority, and moved the python 3.x version at the top position.

Then I reinstalled the pillow, and the problem was solved.

Tân
  • 1
  • 15
  • 56
  • 102
Jane Qin
  • 1
  • 1
0

if pip and conda installing is not working for you try to:

pip3 install pillow

and then:

from PIL import Image

It should work! Check your python version and which one are you using as default! :)

Stephi
  • 39
  • 5
0

Using a conda environment run conda install pillow. If you're using Jupyter notebook, don't forget to restart the Kernel.

This just worked for me.

0

If anyone wondering the above-mentioned process can be implemented using anaconda navigator.

Open the navigator, go to the Environments tab you are currently working and search pillow from the uninstalled tab before the search tab (if it's not installed in the current environment you are working)

Then select pillow and apply. Install then.

After that go to Home tab and launch Spyder.

0

If your using Anaconda 3, u have to install Pillow in the environment that your working on. just go to the not install section and search for Pillow, then install it to the environment. This way will nail the problem for you.

0

Install these:

$ pip install pillow -U

$ pip install pathlib -U
Paul Addai
  • 59
  • 1
  • 7
0

It may be a version issue, I had this issue with TensorFlow 3.2.0 and setting pillow to 8.2.0 worked for me somehow along the way of uninstall/reinstall/restart.

Profiterole
  • 167
  • 1
  • 4