Pyqt5 實時圖像滾動


實時圖像

寫了一個關於實時圖像滾動顯示的例子,做個記錄。

 

滾動算法:

 

難點:

將內存數據繪制到界面,需要用到QImage和QPixmap,使用QImage轉換一下,具體參見代碼。這個費了好大勁才弄出來(網上的資料大都很簡單,處理個QImage打開保存啊等等操作,項目實用性不強。)

 

from PIL import Image

import numpy as np

import threading

import time

 

class GraphicWidget(QWidget):

    def __init__(self):

        super(GraphicWidget,self).__init__()

        self.threadStop = False

        self.drawWidth = 1080

        self.drawHeight = 800

        self.imgWholeData = None

        self.imgScreenData = np.zeros([self.drawHeight,self.drawWidth],np.uint8)

        self.imgTotalLines = 0

        self.imgWidth = 0

        self.threadStop = True

        pass

   

    def doscroll(self):

 

        if self.threadStop:

            img = Image.open("d:/test2.png")

            self.imgWholeData = np.array(img)

            self.imgTotalLines, self.imgWidth = self.imgWholeData.shape

           

            scrollThread = threading.Thread(target = self.scroll)

            scrollThread.start()

 

    def stop(self):

        self.threadStop = True

 

    def scroll(self):

        step = 5

        srcStartLine = 0

        srcEndLine = step

        destEndLine = step

        self.threadStop = False

        while not self.threadStop:

            if destEndLine > self.drawHeight:

                destEndLine = self.drawHeight

 

            if srcEndLine > self.drawHeight:

                srcStartLine = srcEndLine - self.drawHeight

 

            if srcEndLine > self.imgTotalLines:

                print("scroll end")

                self.threadStop = True

                break

            if srcStartLine < self.drawHeight:

                self.imgScreenData = np.zeros([self.drawHeight,self.drawWidth],np.uint8)

           

            self.imgScreenData[0:destEndLine] = self.imgWholeData[srcStartLine:srcEndLine]

 

            self.update()

           

            destEndLine = destEndLine + step

            srcEndLine = srcEndLine + step

 

            time.sleep(0.005)

        pass

 

    def paintEvent(self,event):

        try:

            painter = QPainter()

            destRect = QRect(0,0,self.drawWidth,self.drawHeight)

            srcRect = QRect(0,0,self.drawWidth,self.drawHeight)

            img = QImage(self.imgScreenData.data, self.drawWidth, self.drawHeight, QImage.Format_Indexed8)

            pix = QPixmap.fromImage(img)

            painter.begin(self)

            painter.drawPixmap(destRect, pix, srcRect)

            painter.end()

        except Exception as e:

            print(e)

            raise

       

        Pass

 

經驗證,圖像“拖尾”比較嚴重,圖像滾動平滑效果比不上用DirectX做的效果,但是刷新效率很高,QT這塊做的比C#好很多。

 

下次,實用Opengl做一個,看看效果咋樣。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM