本文介绍了Python图像库:AttributeError:'NoneType'对象没有属性XXX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

推荐答案

通过谷歌搜索,我发现了这个,并且忘记"打开后加载.所以您必须这样做:

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

也请同时对原始评论+1!这个人做的是真正的工作.

这篇关于Python图像库:AttributeError:'NoneType'对象没有属性XXX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:47