Important: Please read the Qt Code of Conduct -

Efficient image rendering from Numpy

  • I just started playing around with PySide2 for visualizing some output from a neural network that produces video frames having never touched Qt before. The resolution can go up to 4K, but is mostly 1080p. I'm wondering if there is any way to make the code below run faster. The code is cobbled together from some examples from StackOverflow:

    import sys
    import numpy as np
    from PySide2 import QtWidgets
    from PySide2 import QtGui
    from PySide2 import QtCore
    class Dataloader:
            def __init__(self):
            def __getitem__(self, idx):
                return np.random.randint(0, 255, size=(1080, 1920, 3), dtype=np.uint8)
            def __len__(self):
                return 1000
    class Viewer(QtWidgets.QWidget):
        """Application main class."""
        def __init__(self, app, dataloader):
            self._app = app
            self._dataloader = dataloader
            self._index = 0
            self._image_widget = None
            self._refresh_timer = None
            self.setWindowTitle("Image Viewer")
        def run(self):
        def refresh(self):
            self._index += 1
            data = self._dataloader[self._index]
            height, width = data.shape[:2]
            qimage = QtGui.QImage(data, width, height, 3 * width, QtGui.QImage.Format_RGB888)
            # Should we just update the image.
            update = self._image_widget is not None
            if not self._image_widget:
                self._image_widget = QtWidgets.QLabel()
            pixmap = QtGui.QPixmap(QtGui.QPixmap.fromImage(qimage))
            if update:
        def _playback_loop(self):
            # Check if playback is still on.
            if not self._play:
            # Still playing so draw new frame.
            # Set timer for next frame.
            self._timer = QtCore.QTimer()
        def _start_playback(self, msecs):
            self._play = True
            self._msecs = msecs
    def main():
        app = QtWidgets.QApplication([])
        dataloader = Dataloader()
        viewer = Viewer(app, dataloader)
    if __name__ == "__main__":

    Not sure if there's a way to do the actual image drawing part more directly from the Numpy array. I have no idea how many memory copies I end up making from the array, since I don't know what goes on under the hood. Can anyone tell me if this is the best I can do with PySide2 without resorting to an OpenGL canvas? (I need to keep the code easily readable by people who only know PyTorch/Numpy/Python)

    (Still need to figure out how to actually draw the image into the main window instead of creating a new one...)

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Good question, I don't currently know what constructor for QImage is used so I can't answer the number of copies being generated. Out of curiosity, what performance do you currently have ?

    PySide2 being pretty new, I suggest you bring this question to the PySide mailing list. You'll find there PySide's developers/maintainers. This forum is more user oriented.

    You might also be interested in the TensorWatch project.

Log in to reply