2013-04-01 33 views
1

我在使用一个QTimer麻烦反复使用NumPy的和PyQt4的

  1. 产生的高度逐宽度×3阵列numpy的
  2. 的numpy的数组转换成重复显示随机噪声的图像一个Qt友好图像,并且
  3. 显示在主Qt的窗口中的图像

(最终的图像不会是随机的。)

这里是相关的代码。

import numpy as np 
from scipy.misc.pilutil import toimage 
from PIL.ImageQt import ImageQt 

def nparrayToQPixmap(arrayImage): 
    pilImage = toimage(arrayImage) 
    qtImage = ImageQt(pilImage) 
    qImage = QtGui.QImage(qtImage) 
    qPixmap = QtGui.QPixmap(qImage) 
    return qPixmap 

class DetectionWidget(QtGui.QWidget): 

    def __init__(self): 

     super(DetectionWidget, self).__init__() 
     self.timer = QtCore.QTimer() 
     self.init_UI() 

    def init_UI(self): 

     self.setFixedSize(self.WIDTH, self.HEIGHT) 
     self.label = QtGui.QLabel(self) 
     self.label.resize(self.WIDTH, self.HEIGHT) 

     self.timer.timeout.connect(self.onTimeout) 

     self.timer.start(1000) 

    def onTimeout(self): 

     npImage = np.random.rand(self.HEIGHT, self.WIDTH, 3) 
     qPixmap = nparrayToQPixmap(npImage) 
     self.label.setPixmap(qPixmap) 

这显示第一图像,而在上self.label.setPixmap(qPixmap)第二迭代的Python段故障。此外,分割,即使我不更新的标签,而是保存使用qPixmap.save(...)的形象,这让我觉得产生的QPixmap是第一次迭代后莫名其妙地损坏故障。

我会感谢任何帮助!

回答

1

这似乎是因为在QImageQPixmap转换中的错误。代码工作,只要QImage是正确的格式..

qImage = QtGui.QImage(qtImage) 

成为

qImage = QtGui.QImage(qtImage).convertToFormat(QtGui.QImage.Format_ARGB32)