Python – image.load_img error in python, Keras

image.load_img error in python, Keras… here is a solution to the problem.

image.load_img error in python, Keras

I have the following code

from keras.preprocessing import image
    test_image = image.load_img('roi.jpg', target_size = (64, 64),grayscale=True)
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)

ROI.jpg are images saved in the same directory. The following error occurs after execution

test_image = image.load_img('roi.jpg', target_size = (64, 64),grayscale=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\preprocessing\image.py", line 345, in load_img raise ImportError('Could not import PIL. Image. ')
ImportError: Could not import PIL. Image. The use of `array_to_img` requires PIL.

Please help me fix this error. I’ve used Windows 10
and python version: Python 3.6.3::Anaconda custom (64-bit).

Solution

You need the PIL module, try the first one and try the second

conda install PIL
conda install Pillow

Or if both PIL and Pillow are present, uninstall them and reinstall PIL

Related Problems and Solutions