14

我用 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'
4

2 回答 2

34

通过谷歌搜索,我在 SO 上找到了这条评论,指出 PIL 有时“懒惰” 并且在打开后“忘记”加载。所以你必须这样做:

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

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

于 2012-09-13T19:44:33.880 回答
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 包管理器。那解决了它。

于 2013-06-27T07:24:53.067 回答