我有如下一段代碼,用於做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了。