[PySide] quick QImage/QPixmap redisplay of changed buffer

Gordon L. Kindlmann glk at uchicago.edu
Wed Nov 10 16:53:16 CET 2021


I'm new to Qt and Pyside, but I'd like to use pyside6 to create a GUI for some C code I'm writing for research. One of the C functions outputs an 8-bit RGB image, in a struct we'll call "foo", with fields "void *data", and "size_t size" for a pointer to the underlying image data buffer and the buffer size, respectively, and fields "unsigned int sx" and "unsigned int sy" for the numbers of columns and rows respectively.  I'm calling this code from python via CFFI, and all that is working fine.

I am also successfully display the image data on the screen with something like (based on https://www.pythonguis.com/faq/adding-images-to-pyqt5-applications/ ):

# ... from CFFI thing import ffi
# ... initialize foo struct with image data

class MyWindow(QMainWindow):
    def __init__(self, foo):
        super(MyWindow, self).__init__()
        self.title = "Image Viewer"
        self.label = QLabel(self)
        self.buff = ffi.buffer(foo.data, foo.size)
        self.image = QImage(self.buff, foo.sx, foo.sy, 3*foo.sx, QImage.Format_RGB888)
        # can modify foo.data here
        self.pixmap = QPixmap.fromImage(self.image)
        # cannot modify foo.data here
        self.resize(self.pixmap.width(), self.pixmap.height())

However, the point of the C library is to quickly change the contents of the image based on other user interactions.

What is the absolute fastest way to redisplay the same pixel buffer (with updated contents)?  Mainly I want to minimize the number of re-allocations happening behind the scenes, and maximize the re-use of existing allocations and objects.  My C library avoids re-allocating the foo.data buffer: the required size of the buffer won't change during the lifetime of MyWindow.

I noticed that I can modify foo.data after I create the QImage; it seems to merely wrap rather than copy the existing buffer.  But once the QPixmap is created via QPixmap.fromImage(); modifying foo.data does nothing (which it makes sense if QPixmap maintains its own internal buffer).

Is there way to tell the QPixmap: "don't allocate anything, just update the values in your internal buffer from the same place you updated them last time", and, to tell the QLabel: "your QPixMap is the same, but its pixel contents have changed, please redisplay"?

Or is there some other very different way of going from my own pixel buffer to something that Pyside displays, that will facilitate the quick re-updates I'm describing?

Thank you for any tips,
Gordon Kindlmann

More information about the PySide mailing list