[Python] PySide6 QImage conversion to PyQtGraph ndarray

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I cannot generate a QImage with some text on it, keep it in memory, and then successfully display it on a pyqtgraph.ImageItem which needs it as an np.ndarray

    import sys
    import numpy as np
    import pyqtgraph as pg
    from PySide6.QtWidgets import QApplication, QMainWindow
    from PySide6.QtGui import QImage, QPainter, QPen, QFont
    from PySide6.QtCore import QSize, QRectF, Qt
    import qimage2ndarray

    class ImageItemExample(QMainWindow):
    def __init__(self):
    super().__init__()
    self.setWindowTitle('PyQtGraph ImageItem Example')
    # Create a GraphicsLayoutWidget
    self.graphics_layout = pg.GraphicsLayoutWidget()
    self.setCentralWidget(self.graphics_layout)
    # Add a PlotItem
    self.plot_item = self.graphics_layout.addPlot()
    # Create an ImageItem
    self.image_item = pg.ImageItem()
    self.plot_item.addItem(self.image_item)

    # Create a QImage
    image = QImage(QSize(400, 300), QImage.Format.Format_RGB32)
    painter = QPainter(image)
    painter.fillRect(QRectF(0, 0, 400, 300), Qt.GlobalColor.black)
    painter.setPen(QPen(Qt.GlobalColor.white))
    painter.setFont(QFont("Courier", 30, QFont.Weight.Bold))
    painter.drawText(QRectF(0, 0, 400, 300), Qt.AlignmentFlag.AlignCenter, "Missing Frame!")
    painter.end()
    image.save("example_image.png") # < picture 1

    # Convert QImage to NumPy array
    image_data = qimage2ndarray.rgb_view(image) # < picture 2
    # image_data = qimage2ndarray.raw_view(image) # < picture 3
    # image_data = qimage2ndarray.recarray_view(image) # doesn't work at all

    # Set the image data
    self.image_item.setImage(image_data)


    if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageItemExample()
    window.show()
    app.exec()


    The picture that gets generated is this:

    [​IMG]

    But the pictures that get generated by the qimage2ndarray look like this:

    [​IMG]

    or this:

    [​IMG]

    or plainly don't work, as in the case of recarray_view

    What am I missing? copilot is useless


    EDIT: As the comments suggested, the result is inconclusive (looks like a memory issue?). In the end I found a different solution without using qimage2nndarray:

    def createImage(text, width=400, height=300, asNdArray=False) -> [QImage, np.ndarray]:
    image = QImage(QSize(width, height), QImage.Format.Format_ARGB32)
    painter = QPainter(image)
    painter.fillRect(QRectF(0, 0, width, height), Qt.GlobalColor.black)
    painter.setPen(QPen(Qt.GlobalColor.white))
    painter.setFont(QFont("Courier", 30, QFont.Weight.Bold))
    painter.drawText(QRectF(0, 0, width, height), Qt.AlignmentFlag.AlignCenter, text)
    painter.end()
    if asNdArray:
    ptr = image.constBits()
    image = np.array(ptr).reshape(image.height(), image.width(), 4)
    return image

    Continue reading...

Compartilhe esta Página