2012-09-13 12 views
12

Abrí una foto con PIL, pero cuando traté de usar split() para dividir los canales llegué siguiente error: AttributeError: 'NoneType' object has no attribute 'bands'bibliotecas de Python Image: AttributeError: objeto 'NoneType' no tiene atributo XXX

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' 

Respuesta

24

Con googleando Encontré este comment on SO, stating that PIL is sometimes 'lazy' y 'olvida' cargar después de abrir. Así que hay que hacerlo de esta manera:

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

favor, 1 también el comentario original! Esta persona hizo el trabajo real.

4

Mi problema fue que PIL no se instaló correctamente. Cuando intento leer un PNG, obtengo ese error. Mi resumen compilación produjo

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

entonces opté por "pip pil desinstalación" y se utiliza el Gestor de paquetes Synaptic en su lugar. Eso lo solucionó

Cuestiones relacionadas