from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap, QImage
import numpy as np
import sys
def print_np():
app = QApplication(sys.argv)
win = QWidget()
label = QLabel()
img = np.ones((500, 500))
qImg = QPixmap(QImage(img.data, img.shape[0], img.shape[1], QImage.Format_Indexed8))
label.setPixmap(qImg)
vbox = QVBoxLayout()
vbox.addWidget(label)
win.setLayout(vbox)
win.show()
sys.exit(app.exec_())
In this code, I am trying to display 2d np array on QtWindow.
I am expecting to see a white square, but I keep getting a weird display as below.
I believe its because wrong QImage.Format (data type), so I tried every option but didn't help.
https://srinikom.github.io/pyside-docs/PySide/QtGui/QImage.html#PySide.QtGui.QImage
Should I change np array to jpg or png first, as examples in documentation?



#importstatements? It makes your code impossible to run and it's therefore harder to assist you.500,500,3to accommodate 3 RGB channels and have type RGB888. If you only want black/white/greys, your shape is correct but you need to tell Qt is is greyscale.img = np.ones((500, 500), dtype=dtype=np.uint8). And maybe you also want the RGB versionimg = np.ones((3, 500, 500) ...