Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error "Could not import PIL.Image. The use of `array_to_img` requires PIL."

I am a beginner to machine learning, so I was trying to create a model for recognizing images referenced from Keras blog. I have installed Anaconda 3 on windows 10 and all the packages like tensorflow, keras, scipy, numpy, pandas

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
    from keras.models import Sequential
    from keras.layers import Conv2D, MaxPooling2D
    from keras.layers import Activation, Dropout, Flatten, Dense

    datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')
    img = load_img('E:/ML_R&D/training_set/cats/cat.3919.jpg') # this Line is giving me error

I am using conda command and pillow using pip, but when I run a code taken from keras blog I got the error.

like image 281
Rahul Avatar asked Jul 23 '18 12:07

Rahul


People also ask

Does the use of `array_to_IMG` require PIL?

The use of `array_to_img` requires PIL - Jetson Nano - NVIDIA Developer Forums Could not import PIL.Image. The use of `array_to_img` requires PIL

Does load_IMG require PIL import?

The use of load_img requires PIL #444 ImportError: Could not import PIL.Image. The use of load_img requires PIL. I am running on MacOS 11.0.1 with the Apple TensorFlow backend. My reticulate::py_config () returns the following result:

What is error 0 StopIteration in PIL?

0 StopIteration: Could not import PIL.Image. The use of `array_to_img` requires PIL error 2 Not able to print correct confusion matrix and also in heatmap values are printing in example 2e+2, e+4 etc etc


3 Answers

You will want to install the PIL package.

How do I install PIL/Pillow for Python 3.6?

pip install pillow

or

pip3 install pillow
like image 52
WestCoastProjects Avatar answered Jan 04 '23 10:01

WestCoastProjects


the issue is solved, may be the problem was that I did not restarted my system after adding all the libraries. This issue can be closed.

like image 30
Rahul Avatar answered Jan 04 '23 11:01

Rahul


Step 1: Firstly make sure that the PIL is uninstalled as PIL and pillow cannot co-exist. For this open Anaconda Powershell Prompt and type pip uninstall pillow Then press y(yes) when asked. If you do not have pillow or PIL then proceed to step 2

Step 2: In the same powershell prompt, execute pip install pillow

Step 3: After the installation, close the jupyter notebook, anaconda, and restart your system.

Step 4: Now execute from keras.preprocessing.image import load_image . IT WILL WORK !!!

like image 21
Anirudh RVS Avatar answered Jan 04 '23 11:01

Anirudh RVS