I have a matrix array with 3 channels (RGB) named rgb_array
.
I want to use PyQt to display it.
Following to some (old) posts in stackoverflow, I made the following code (based on gym-minigrid)
from PyQt5.QtGui import QPixmap
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QMainWindow, QWidget
class Window(QMainWindow):
"""
Simple application window to render the environment into
"""
def __init__(self):
super().__init__()
# Image label to display the rendering
self.imgLabel = QLabel()
# Create a main widget for the window
mainWidget = QWidget(self)
self.setCentralWidget(mainWidget)
# Show the application window
self.show()
self.setFocus()
def setPixmap(self, pixmap):
self.imgLabel.setPixmap(pixmap)
rgb_array = ... # my image
height, width, _ = rgb_array.shape
bytes_per_line = 3 * width
qt_img = QImage(rgb_array.data, width, height, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qt_img)
window = Window()
window.setPixmap(pixmap)
But everytime I run it, I get a segmentation fault. Any idea ? Thanks !