【发布时间】:2019-02-01 14:23:52
【问题描述】:
这非常棘手。我几乎尝试了互联网上的所有解决方案,但仍然无法解决这个问题。
如果我只是输入
from PIL import ImageFile
print(Image.__file__)
一切正常,系统将打印
C:\Users\BenWang\Anaconda3\envs\tensorflow\lib\site-packages\PIL\Image.py
这样
但在我的编码中,如果我使用以下代码:
import sys
from PIL import Image
ImageFile.LOAD_TRUNCATED_IMAGES = True
train_tensors = paths_to_tensor(train_files).astype('float32')/255
valid_tensors = paths_to_tensor(valid_files).astype('float32')/255
test_tensors = paths_to_tensor(test_files).astype('float32')/255
错误将显示如下:
ImportError Traceback (most recent call last)
<ipython-input-166-10a043c54408> in <module>()
4
5
----> 6 train_tensors = paths_to_tensor(train_files).astype('float32')/255
7 valid_tensors = paths_to_tensor(valid_files).astype('float32')/255
8 test_tensors = paths_to_tensor(test_files).astype('float32')/255
<ipython-input-149-36af157e68bd> in paths_to_tensor(img_paths)
11
12 def paths_to_tensor(img_paths):
---> 13 list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
14 return np.vstack(list_of_tensors)
<ipython-input-149-36af157e68bd> in <listcomp>(.0)
11
12 def paths_to_tensor(img_paths):
---> 13 list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
14 return np.vstack(list_of_tensors)
<ipython-input-149-36af157e68bd> in path_to_tensor(img_path)
4 def path_to_tensor(img_path):
5
----> 6 img = image.load_img(img_path, target_size=(224, 224))
7
8 x = image.img_to_array(img)
~\Anaconda3\envs\tensorflow\lib\site-packages\keras_preprocessing\image\utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
100 color_mode = 'grayscale'
101 if pil_image is None:
--> 102 raise ImportError('Could not import PIL.Image. '
103 'The use of `array_to_img` requires PIL.')
104 img = pil_image.open(path)
ImportError: Could not import PIL.Image. The use of `array_to_img` requires PIL.
【问题讨论】:
-
Keras和PIL在同一个环境吗?
-
@DanielMöller 我认为他们是
-
你试过这个解决方案吗:stackoverflow.com/questions/41124353/…?
-
@LucaMassaron 是的,我已经尝试过这些,包括卸载然后安装枕头和安装 keras。而且因为我在一台新电脑上瘦身,所以没有安装以前的 python。
-
重启内核。
标签: python keras jupyter-notebook python-imaging-library