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@qt-project.org
https://lists.qt-project.org/listinfo/pyside
_______________________________________________
PySide mailing list
PySide@qt-project.org
https://lists.qt-project.org/listinfo/pyside

Reply via email to