Python Image Library: AttributeError: 'NoneTyp

2019-02-08 16:33发布

I opened a picture with PIL, but when I tried to use split() to split the channels I got following error: 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条回答
劳资没心,怎么记你
2楼-- · 2019-02-08 17:06

My problem was that PIL was not installed correctly. When trying to read a png I'd get that error. My compilation summary yielded

--------------------------------------------------------------------
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
--------------------------------------------------------------------

I then opted to "pip uninstall pil" and used the Synaptic Package Manager instead. That fixed it.

查看更多
小情绪 Triste *
3楼-- · 2019-02-08 17:28

With googling I found this comment on SO, stating that PIL is sometimes 'lazy' and 'forgets' to load after opening. So you have to do it like this:

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

Please +1 also the original comment! This person did the real work.

查看更多
登录 后发表回答