导入错误:需要 Python 图像库 (PIL) 才能从 jpeg 文件加载数据
Posted
技术标签:
【中文标题】导入错误:需要 Python 图像库 (PIL) 才能从 jpeg 文件加载数据【英文标题】:Import Error: The Python Imaging Library (PIL) is required to load data from jpeg files 【发布时间】:2018-08-10 23:59:27 【问题描述】:我正在尝试使用 scikit-learn 从LFW dataset 获取数据:
from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people(min_faces_per_person=60)
这样做时,我会收到 导入错误 消息:
从 jpeg 文件加载数据需要 Python Imaging Library (PIL)
错误信息表明我需要安装pillow。所以我从我的 Jupyter 笔记本安装了pillow
,如下所示:
!conda install --yes --prefix sys.prefix pillow
现在我可以执行import PIL
,但在尝试从 LFW 数据集中获取数据时仍然收到相同的错误消息。
我正在使用 Python 3.6。这是完整的跟踪:
ImportError Traceback (most recent call last) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\datasets\lfw.py in _load_imgs(file_paths, slice_, color, resize)
143 try:
--> 144 from scipy.misc import imread
145 except ImportError:
ImportError: cannot import name 'imread'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\misc\pilutil.py in <module>()
18 try:
---> 19 from PIL import Image, ImageFilter
20 except ImportError:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\PIL\Image.py in <module>()
57 # and should be considered private and subject to change.
---> 58 from . import _imaging as core
59 if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
ModuleNotFoundError Traceback (most recent call last) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\datasets\lfw.py in _load_imgs(file_paths, slice_, color, resize)
145 except ImportError:
--> 146 from scipy.misc.pilutil import imread
147 from scipy.misc import imresize
~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\misc\pilutil.py in <module>()
20 except ImportError:
---> 21 import Image
22 import ImageFilter
ModuleNotFoundError: No module named 'Image'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last) <ipython-input-236-6d1c601c92a3> in <module>()
----> 1 faces = fetch_lfw_people(min_faces_per_person=60)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\datasets\lfw.py in fetch_lfw_people(data_home, funneled, resize, min_faces_per_person, color, slice_, download_if_missing)
333 faces, target, target_names = load_func(
334 data_folder_path, resize=resize,
--> 335 min_faces_per_person=min_faces_per_person, color=color, slice_=slice_)
336
337 # pack the results as a Bunch instance
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\externals\joblib\memory.py in __call__(self, *args, **kwargs)
560
561 def __call__(self, *args, **kwargs):
--> 562 return self._cached_call(args, kwargs)[0]
563
564 def __reduce__(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\externals\joblib\memory.py in _cached_call(self, args, kwargs)
508 'directory %s'
509 % (name, argument_hash, output_dir))
--> 510 out, metadata = self.call(*args, **kwargs)
511 if self.mmap_mode is not None:
512 # Memmap the output at the first call to be consistent with
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\externals\joblib\memory.py in call(self, *args, **kwargs)
742 if self._verbose > 0:
743 print(format_call(self.func, args, kwargs))
--> 744 output = self.func(*args, **kwargs)
745 self._persist_output(output, output_dir)
746 duration = time.time() - start_time
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\datasets\lfw.py in _fetch_lfw_people(data_folder_path, slice_, color, resize, min_faces_per_person)
234 target = np.searchsorted(target_names, person_names)
235
--> 236 faces = _load_imgs(file_paths, slice_, color, resize)
237
238 # shuffle the faces with a deterministic RNG scheme to avoid having
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\datasets\lfw.py in _load_imgs(file_paths, slice_, color, resize)
147 from scipy.misc import imresize
148 except ImportError:
--> 149 raise ImportError("The Python Imaging Library (PIL)"
150 " is required to load data from jpeg files")
151
ImportError: The Python Imaging Library (PIL) is required to load data from jpeg files
为什么我会收到此导入错误,我该如何解决?
【问题讨论】:
【参考方案1】:您需要在代码中添加以下行
from PIL import Image
【讨论】:
【参考方案2】:额外细节:
Scikit-learn 使用pillow
加载图像。
根据 datasets/base.py 中的 sklearn 代码,pillow
将根据要求导入:
# import PIL only when needed
from ..externals._pilutil import imread
...如果未安装库,则会失败。通过例如安装它conda,执行以下命令:
> conda install --yes --prefix <environment_path_obtained_from_runnig_sys.prefix> pillow
在您重新启动内核/刷新 IDE 以包含新安装后,无需导入库或其任何组件,除非您希望在 sklearn 正在执行的操作之外处理图像。
【讨论】:
【参考方案3】:我遇到了同样的问题。重新启动内核不起作用,但重新启动 Jupyter Lab 可以。我不确定为什么会发生这种情况。
【讨论】:
以上是关于导入错误:需要 Python 图像库 (PIL) 才能从 jpeg 文件加载数据的主要内容,如果未能解决你的问题,请参考以下文章