2013-04-01 108 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)