Considering the following code
from PyQt5.QtWidgets import QMainWindow, QLabel, QSizePolicy, QApplication from PyQt5.QtGui import QPixmap, QImage from PyQt5.QtCore import Qt import numpy as np import sys class Test(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(10,10,640, 400) pixmap_label = QLabel() pixmap_label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) pixmap_label.resize(640,400) pixmap_label.setAlignment(Qt.AlignCenter) im_np = np.ones((1800,2880,3),dtype=uint8) im_np = np.transpose(im_np, (1,0,2)) qimage = QImage(im_np, im_np.shape[1], im_np.shape[0], QImage.Format_RGB888) pixmap = QPixmap(qimage) pixmap = pixmap.scaled(640,400, Qt.KeepAspectRatio) pixmap_label.setPixmap(pixmap) self.setCentralWidget(pixmap_label) self.show() def main(): app = QApplication(sys.argv) win = Test() sys.exit(app.exec_()) if __name__=="__main__": main()
I get the following error
TypeError: arguments did not match any overloaded call: QImage(): too many arguments QImage(QSize, QImage.Format): argument 1 has unexpected type ‘numpy.ndarray’ QImage(int, int, QImage.Format): argument 1 has unexpected type ‘numpy.ndarray’ QImage(bytes, int, int, QImage.Format): argument 1 has unexpected type ‘numpy.ndarray’
QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type ‘numpy.ndarray’ QImage(bytes, int, int, int, QImage.Format): argument 1 has unexpected type ‘numpy.ndarray’
QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type ‘numpy.ndarray’ QImage(List[str]): argument 1 has unexpected type ‘numpy.ndarray’ QImage(str, format: str = None): argument 1 has unexpected type ‘numpy.ndarray’ QImage(QImage): argument 1 has unexpected type ‘numpy.ndarray’ QImage(Any): too many arguments
According to this post this can be caused by numpy creating a view. Modifying the lines
im_np = np.array(img) im_np = np.transpose(im_np, (1,0,2))
To
im_np = np.array(img) im_np = np.transpose(im_np, (1,0,2)) im_np_cpy = np.copy(im_np)
Produces the same error. To test that I am not passing a view I print the result of the test
im_np_cpy.base is im_np
and it’s False. The image is visualised correctly with cv2. I am clearly missing something, any idea what?
Cheers!
Advertisement
Answer
I added a copy after the transpose like this:
im_np = np.transpose(im_np,(1,0,2)).copy()
and that worked for me.