Python Image Library:AttributeError:'NoneType'对象没有属性XXX

时间:2012-09-13 19:44:34

标签: python-imaging-library

我用PIL打开了一张图片,但是当我尝试使用split()来分割频道时出现以下错误: AttributeError: 'NoneType' object has no attribute 'bands'

import Image
img = Image.open('IMG_0007.jpg')

img.split()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/blum/<ipython console> in <module>()

/usr/lib/python2.6/dist-packages/PIL/Image.pyc in split(self)
   1495         "Split image into bands"
   1496 
-> 1497         if self.im.bands == 1:
   1498             ims = [self.copy()]
   1499         else:

AttributeError: 'NoneType' object has no attribute 'bands'

2 个答案:

答案 0 :(得分:27)

通过Google搜索,我发现此comment on SO, stating that PIL is sometimes 'lazy'和'忘记'在打开后加载。所以你必须这样做:

import Image
img = Image.open('IMG_0007.jpg')
img.load()
img.split()

也请原来评论+1!这个人做了真正的工作。

答案 1 :(得分:4)

我的问题是PIL安装不正确。在尝试阅读png时,我会收到该错误。我的汇编摘要产生了

--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.7.3 (default, Apr 21 2012, 01:05:55)
              [GCC 4.6.3]
--------------------------------------------------------------------
*** TKINTER support not available
*** JPEG support not available
*** ZLIB (PNG/ZIP) support not available <===============
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------

然后我选择了“pip uninstall pil”并使用了Synaptic Package Manager。修好了。