Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show and update a bitmap FAST in Python?

I have a device from which I read images. Every image is an array of 8b grayscale pixels row by row. How can I display the image sequence as a video?

Something like this:

while !terminated:
    image = readImage(...)
    widget.updateImage(image, width, height)

Specifications:

  • must be implemented using PyQt
like image 979
Adam Trhon Avatar asked Sep 19 '13 15:09

Adam Trhon


1 Answers

My best solution with PyQt:

#! /usr/bin/python2

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class Preview(QtGui.QWidget):

    def __init__(self):
        super(Preview, self).__init__()

        self.setWindowTitle('Preview')

        self.previewImage = QtGui.QLabel(self)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.previewImage)
        self.setLayout(vbox)

        self.updater = QtCore.QTimer()
        self.updater.setSingleShot(True)
        self.updater.setInterval(10)
        self.updater.timeout.connect(self.update)

        self.show()
        self.updater.start()

    def update(self):
        data = get_pgm(...) # this function reads device and adds PGM header
        pixmap = QtGui.QPixmap()
        pixmap.loadFromData(data)
        self.previewImage.setPixmap(pixmap)
        self.previewImage.show()
        self.updater.start()


def main():
    app = QtGui.QApplication(sys.argv)
    preview = Preview()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
like image 68
Adam Trhon Avatar answered Oct 16 '22 02:10

Adam Trhon