Skip to main content
 首页 » 编程设计

python之python,Keras 中的 image.load_img 错误

2024年09月07日8haluo1

我有以下代码

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是保存在同一目录下的图片。执行后出现如下错误

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. 

请帮我解决这个错误。我用过 windows 10 和 python 版本:Python 3.6.3::Anaconda 自定义(64 位)

请您参考如下方法:

你需要PIL模块,试试第一个再试试第二个

conda install PIL 
conda install Pillow 

或者如果 PIL 和 Pillow 都存在,则卸载它们并重新安装 PIL