在PySide制造图像(QImage)的最有效的方式是什么?
我的测试形象是1,9MB, 3648x2736px, jpegpho/strong>。
我尝试了两种方式:
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
平均时间=4
Better but still slow. Is there a faster way to calculate image histogram from QImage?