我有如下一段代码,用于做tumbnail,
mode = image.mode if mode not in ('L','RGB'): if mode == 'RGBA': alpha = image.split()[3] bgmask = alpha.point(lambda x:255-x) image = image.convert('RGB') image.paste((255,255,255),None,bgmask) else: image = image.convert('RGB')
当遇到图片mode 是 RGBA时,就会报错:
'NoneType' object has no attribute 'bands'
上网一查,才知道是pil的一个bug。解决方法很好办:
vim /usr/local/lib/python2.7/dist-packages/PIL/Image.py 定位到1501 行 1494 def split(self): 1495 "Split image into bands" 1496 14971498 if self.im.bands == 1: 1499 ims = [self.copy()] 1500 else: 1501 self.load() 1502 ims = [] 1503 for i in range(self.im.bands): 1504 ims.append(self._new(self.im.getband(i))) 1505 return tuple(ims)
1494 def split(self): 1495 "Split image into bands" 1496 1497 self.load() 1498 if self.im.bands == 1: 1499 ims = [self.copy()] 1500 else: 1501 #self.load() 1502 ims = [] 1503 for i in range(self.im.bands): 1504 ims.append(self._new(self.im.getband(i))) 1505 return tuple(ims)
这样子就可以解决掉这个bug了。