在PySide中创建图像(QImage)直方图的最有效方法是什么?
我的测试图片 1,9MB,3648x2736px,jpeg照片
我尝试了两种方法:
1
import time
start = time.time()
for y in range(h):
line = img.scanLine(y) # img - instance of QImage
for x in range(w):
color = struct.unpack('I', line[x*4:x*4+4])[0]
self.obrhis[0][QtGui.qRed(color)] += 1 # red
self.obrhis[1][QtGui.qGreen(color)] += 1 # green
self.obrhis[2][QtGui.qBlue(color)] += 1 # blue
print 'time: ', time.time() - start
平均时间= 15秒
2
import time
start = time.time()
buffer = QtCore.QBuffer()
buffer.open(QtCore.QIODevice.ReadWrite)
img.save(buffer, "PNG")
import cStringIO
import Image
strio = cStringIO.StringIO()
strio.write(buffer.data())
buffer.close()
strio.seek(0)
pilimg = Image.open(strio)
hist = pilimg.histogram()
self.obrhis[0] = hist[:256]
self.obrhis[1] = hist[256:512]
self.obrhis[2] = hist[512:]
print 'time: ', time.time() - start
平均时间= 4s
更好但仍然缓慢。 有没有更快的方法从QImage计算图像直方图?