[PySide] quick QImage/QPixmap redisplay of changed buffer

icfwm at gmx.net icfwm at gmx.net
Fri Nov 12 20:25:32 CET 2021

starting point would be probably this
https://doc.qt.io/qt-5/topics-graphics.html or this

Am 11/12/21 um 1:01 AM schrieb Gordon L. Kindlmann:
> Hello,
> Thank you very much for this tip.  It works!  I've confirmed this by acting on mouse events, and implementing a simple image operation in C, and repainting.  It's very zippy (though for all I know my older method with higher overhead would have felt just as zippy). But if this becomes a bottle-neck I'll look accessing the QImage bits() directly.
> One follow-up question:
> Can you suggest a documentation/tutorial source that would have let me find something like your tip (or its ingredients)?  My (possibly naive) web searches about how to display pixel data on pyside led to the QImage->QPixmap->QLabel process I first described, and I'm curious what else I should have searched for.
> Thanks,
> Gordon
>> On Nov 11, 2021, at 12:07 PM, icfwm at gmx.net wrote:
>> Hello,
>> using a QLabel and QLabel.setPixmap doesn't seem to be the fastest way
>> to display the updated image. I'd try to go this path:
>> Derive your own class from QWidget and overwrite the paintEvent(...)
>> method on that:
>>      class MyWidget(QWidget):
>>          # ...
>>          def paintEvent(self, p):
>>              p = QPainter(self)
>>              p.drawImage(0,0,self.image)
>>              p.end()
>> where self.image is the QImage instance holding the current image to be
>> displayed. With this approach you save at least one copy for converting
>> QImage to QPixmap.
>> Note that it might be best to write a C function, which gets the QImage
>> buffer pointer passed (image.bits() should give you access to that) to
>> operate directly on the QImage buffer. In principle this should be
>> possible with the pyhon buffer interface. I am not exactly sure how to
>> achieve this with cffi (I usually use ctypes for this kind of stuff).
>> Hope this helps
>> Am 11/10/21 um 4:53 PM schrieb Gordon L. Kindlmann:
>>> Hello,
>>> 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.setWindowTitle(self.title)
>>> 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.label.setPixmap(self.pixmap)
>>> self.setCentralWidget(self.label)
>>> 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
>>> _______________________________________________
>>> PySide mailing list
>>> PySide at qt-project.org
>>> https://lists.qt-project.org/listinfo/pyside
>> _______________________________________________
>> PySide mailing list
>> PySide at qt-project.org
>> https://lists.qt-project.org/listinfo/pyside
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> https://lists.qt-project.org/listinfo/pyside

More information about the PySide mailing list