2014-11-03 118 views
14

的另一个版本内置我得到的错误:PIL:导入错误:将_imaging扩展是为枕头或PIL

--------------------------------------------------------------------------- 
ImportError        Traceback (most recent call last) 
<ipython-input-4-0f6709e38f49> in <module>() 
----> 1 from PIL import Image 

C:\Anaconda\lib\site-packages\PIL\Image.py in <module>() 
    61  from PIL import _imaging as core 
    62  if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None): 
---> 63   raise ImportError("The _imaging extension was built for another " 
    64       " version of Pillow or PIL") 
    65 

ImportError: The _imaging extension was built for another version of Pillow or PIL 

每当我尝试使用PIL库。我试图加载和一堆.gif注意的的工作,我尝试了,如下:

from PIL import Image 

尝试了不同的方法,通过与SciPy的:

import scipy.ndimage as spnd 
os.chdir('C:\\WeatherSink\\data\\') 
spnd.imread('2014-11-03-0645.gif') 

不符合:

--------------------------------------------------------------------------- 
ImportError        Traceback (most recent call last) 
<ipython-input-3-23c383b79646> in <module>() 
     1 os.chdir('C:\\WeatherSink\\data\\') 
----> 2 spnd.imread('2014-11-03-0645.gif') 

C:\Anaconda\lib\site-packages\scipy\ndimage\io.pyc in imread(fname, flatten, mode) 
    36   from PIL import Image 
    37  except ImportError: 
---> 38   raise ImportError("Could not import the Python Imaging Library (PIL)" 
    39       " required to load image files. Please refer to" 
    40       " http://pypi.python.org/pypi/PIL/ for installation" 

ImportError: Could not import the Python Imaging Library (PIL) required to load image files. Please refer to http://pypi.python.org/pypi/PIL/ for installation instructions. 

第一种方法指导我安装PIL版本。我尝试模拟getattr(...),并返回None。所以我并不感到惊讶,因为它不起作用。 但有谁知道如何'修复'的错误?

我在win7上运行,通过conda管理python2.7。我试图删除并重新安装这些软件包,而不会改变输出。

非常感谢帮助。

+1

首先猜测你是否安装了多个版本的PIL或Pillow,并且可能在不同的环境下安装。找到他们全部,全部卸载它们,然后重新安装一个新的枕头。 – Hugo 2014-11-04 10:14:13

回答

3

也许你的一个依赖项需要PIL和PIL最终在Pillow之后安装,导致你的站点包dir中出现冲突。我假设你看到了这个错误,因为导入语句是从合法的PIL安装导入_imaging而不是Pillow安装。

我在过去遇到过需要PIL或Pillow的冲突包的麻烦。枕头当然是首选套餐。我会看看你的包的依赖关系。如果你能找到一个依赖于PIL的函数,我会提交一个pull请求,它将依赖关系改变为Pillow,或者甚至可以用这个改变创建你自己的fork。对于我的情况,分叉是我选择的选择,因为该项目似乎很长一段时间没有任何活动。

最终,您希望消除对PIL包的任何依赖关系(因为它不再处于活动状态)以支持Pillow。

+0

http://scikit-learn.org/stable/auto_examples/applications/face_recognition.html – 2014-12-31 22:45:53

+3

Jan,我应该在该页面上寻找什么? – 2014-12-31 22:47:42

+0

是的,请:) :) – 2014-12-31 22:53:23

16

这只是一个安装问题。 在你的系统首先安装PIP如果不是insalled它可用于Windows也https://pip.pypa.io/en/latest/installing.html

升级numpy的,PIP /枕头,SciPy的

PIP安装-U numpy的

PIP安装-U PIL /枕头

PIP安装-U SciPy的

或最好的选择窗口是使用anaconda

我认为pip已经安装在conda中。这将解决您的系统版本问题。

In [1]: from PIL import Image 

In [2]: import scipy.ndimage as spnd 

In [3]: x = spnd.imread('ppuf100X91.gif') 

In [4]: print x 
[[255 255 255 ..., 255 255 255] 
[255 255 255 ..., 255 255 255] 
[255 255 255 ..., 255 255 255] 
..., 
[255 255 255 ..., 255 255 255] 
[255 255 255 ..., 255 255 255] 
[255 255 255 ..., 255 255 255]] 
+1

它解决了我的问题,thx!我不得不使用'pip install -U pillow''('pip install -U pil'给''找不到满足需求pil'的版本)。 – 2016-01-22 16:16:34

3

这个问题是因为Python包PIL /枕头是向上或系统的缩小版本,并且由于这个问题是在你的系统中生成。

尝试检查这个命令:

sudo apt-get install python-PIL 

检查这个包是安装或不安装。如果安装不是试图用命令删除:

sudo apt-get remove python-PIL 

入住这将努力消除在PIL /枕头包从系统中删除。

最后这个命令将帮助你解决这个问题包:

sudo apt-get autoremove python-PIL 

然后重新安装PIL /枕头包:

sudo apt-get install python-pil 

这会帮助你解决问题。

1

这是在Python 3.6 编辑文件的一个问题:C:\蟒蛇\ LIB \站点包\ PIL \ Image.py 和更改代码: 如果PILLOW_VERSION = GETATTR(核心, 'PILLOW_VERSION',无! ): 提高导入错误(以下简称 “_imaging扩展建另一个” “版本枕头或PIL的”)

变化,为: 如果core.PILLOW_VERSION = GETATTR(核心, 'PILLOW_VERSION',无)! raise ImportError(“_imaging扩展是为另一个” “版本的Pillow或PIL构建的)

这将解决问题。 Regards

+0

嗨易卜拉欣,这是否也适用于Python 2.7?你能否强调两个码位之间的区别? – 2017-05-05 13:16:22

+0

是的,在python 2.7中,它有同样的问题。问题是它导入了所有带有名称空间核心的符号,并且在没有名称空间的情况下寻址常量 – ibrahim 2017-05-07 02:01:00