Python 优化PIL/numpy例程

Python 优化PIL/numpy例程,python,numpy,optimization,python-imaging-library,Python,Numpy,Optimization,Python Imaging Library,我正在处理一个被清理成黑白的PIL图像。图像约为200x200像素 这个过程就是这样的,我只需要图像中黑色像素的索引 self.image = Image.open(path) npArray = numpy.array(Image.open(self.image.getdata(), numpy.uint8).reshape(self.image.size[1], self.image.size[0], 4) black

我正在处理一个被清理成黑白的PIL图像。图像约为200x200像素

这个过程就是这样的,我只需要图像中黑色像素的索引

self.image = Image.open(path)
npArray = numpy.array(Image.open(self.image.getdata(),
                                   numpy.uint8).reshape(self.image.size[1], self.image.size[0], 4)

black = [(i, j) for i in range(1,dim-1) for j in range(1,dim-1) if  npArray[i][j][0] == 0]

return numpy.asarray(black)

在大多数情况下,它是有效的,但却是性能的瓶颈。有什么办法可以加快速度吗?

你在使用Python3吗?
xind,yind=np。where(img[…,0]==0)
可以做你想做的事(我手头没有黑白图像可供验证)。或者,如果你想让坐标成对,可以使用
np.argwhere(img[…,0]==0)
@PaulPanzer Nice,我还不知道
argwhere
。感谢argwhere将我机器上的时间从16秒缩短到5秒。