Search code examples
pythonqtpyqt4

QPixmap white display issue


I am trying to show OpenCV images on GraphicsView Widget on Qt with the following python code:

frame = self.frames[number]
height, width, channel = frame.shape
bytesPerLine = 3*width
qImg = QtGui.QImage(frame.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB32)
self.scene.addPixmap(QPixmap(qImg))
self.graphicsView.setScene(self.scene)
self.graphicsView.show()

The code above shows the image on GraphicsView Widget as totally white, or it doesn't show at all, because GraphicsView Widget background is white.

However, when I change the following code from

qImg = QtGui.QImage(frame.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB32)

to

qImg = QtGui.QImage(frame.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888),

the image is displayed on the widget. However, the image quality is not the one I need.

I have to show my image on RGB quality, without changing any color values on pixels. It is possible by showing my image with RGB32 format. How I can manage that?

Best,


Solution

  • The constructor you are using for QImage won't do any conversion. So the format parameter must match the format of your data. Your data is in 24 bit format, and so you need to use the QtGui.QImage.Format_RGB888.

    In any case, the 32 bit format does not differ in quality - it just has an additional 0xff in front of each 24 bit value.

    No other format code is going to work. If you have a quality problem you might need to look elsewhere. But you could convert the image to a different format using convertToFormat.