It has 2 errors:
- You have to pass the
bytesPerLine
to the QImage
- You have an "n" of more in
trainOpenImg
when you set the alignment.
def displayImage(self):
qformat =QImage.Format_Indexed8
if len(self.image.shape)==3:
if self.image.shape[2] ==4:
qformat=QImage.Format_RGBA8888
else:
qformat=QImage.Format_RGB888
img = QtGui.QImage(self.image.data,
self.image.shape[1],
self.image.shape[0],
self.image.strides[0], # <--- +++
qformat)
img = img.rgbSwapped()
self.trainOpenImg.setPixmap(QPixmap.fromImage(img))
self.trainOpenImg.setAlignment(QtCore.Qt.AlignCenter)
On the other hand IDEs have problems handling certain types of errors and only launch a code, so in those cases it is advisable to run it in the CMD or terminal as they give you more information, for example in this case the error message was :
Traceback (most recent call last):
File "test.py", line 20, in loadClicked
self.loadImage(fname)
File "test.py", line 26, in loadImage
self.displayImage()
File "test.py", line 36, in displayImage
img=QtGui.QImage(self.image.data,self.image.shape[1],self.image[0],QtGui.QImage.Format_RGB888)
TypeError: arguments did not match any overloaded call:
QImage(): too many arguments
QImage(QSize, QImage.Format): argument 1 has unexpected type 'memoryview'
QImage(int, int, QImage.Format): argument 1 has unexpected type 'memoryview'
QImage(bytes, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(bytes, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, int, QImage.Format): argument 3 has unexpected type 'numpy.ndarray'
QImage(List[str]): argument 1 has unexpected type 'memoryview'
QImage(str, format: str = None): argument 1 has unexpected type 'memoryview'
QImage(QImage): argument 1 has unexpected type 'memoryview'
QImage(Any): too many arguments
Aborted (core dumped)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…