Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
6.5.1
-
None
-
Windows 10, PySide6 6.5.1.1
Description
I am heavily using opencv in my application. Which works in BGR/BGRA format. QImage supports BGR888 (https://doc.qt.io/qt-6/qimage.html#Format-enum), but unfortunately not BGRA8888. See the following code in Python (using opencv-python-healdess and PySide6-Essentials):
def set_preview_image(qlabel: QLabel, image: cv2.typing.MatLike): height, width, channels = image.shape if channels == BGRA_CHANNEL_COUNT: image_format = QtGui.QImage.Format.Format_RGBA8888 capture = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA) else: image_format = QtGui.QImage.Format.Format_BGR888 capture = image qimage = QtGui.QImage(capture.data, width, height, width * channels, image_format) qlabel.setPixmap( QtGui.QPixmap(qimage).scaled( qlabel.size(), QtCore.Qt.AspectRatioMode.IgnoreAspectRatio, QtCore.Qt.TransformationMode.SmoothTransformation, ), )
Could be simplified to:
def set_preview_image(qlabel: QLabel, image: cv2.typing.MatLike): height, width, channels = image.shape image_format = QtGui.QImage.Format.Format_BGRA8888 \ if channels == BGRA_CHANNEL_COUNT \ else QtGui.QImage.Format.Format_BGR888 qimage = QtGui.QImage(image.data, width, height, width * channels, image_format) qlabel.setPixmap( QtGui.QPixmap(qimage).scaled( qlabel.size(), QtCore.Qt.AspectRatioMode.IgnoreAspectRatio, QtCore.Qt.TransformationMode.SmoothTransformation, ), )