Rahul
Rahul

Reputation: 279

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.

Upvotes: 6

Views: 15096

Answers (4)

Anirudh RVS
Anirudh RVS

Reputation: 41

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 !!!

Upvotes: 4

Rahul
Rahul

Reputation: 279

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.

Upvotes: 4

karan
karan

Reputation: 81

You didn't import the library. Use "import PIL"

Upvotes: 0

WestCoastProjects
WestCoastProjects

Reputation: 63281

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

Upvotes: 7

Related Questions