PyQt - 使用标签显示图像

问题描述 投票:2回答:1

我已经将一个numpy数组转换为pixmap,以便在我的GUI中的标签上显示它。当我运行程序时,GUI因某种原因关闭(没有错误消息)。

height, width = input_image.shape
bytesPerLine = 3 * width
qImg = QtGui.QImage(input_image.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
pixmap01 = QtGui.QPixmap.fromImage(qImg)
self.pixmap_image = QtGui.QPixmap(pixmap01)
self.ui.label_imageDisplay.setPixmap(self.pixmap_image)
self.ui.label_imageDisplay.setAlignment(QtCore.Qt.AlignCenter)
self.ui.label_imageDisplay.setScaledContents(True)
self.ui.label_imageDisplay.setMinimumSize(1,1)
self.ui.label_imageDisplay.show()
python pyqt pyqt5 qimage qlabel
1个回答
3
投票

试试这个:

from PyQt5 import QtWidgets, QtGui, QtCore

from scipy.ndimage import imread
import sys

app = QtWidgets.QApplication(sys.argv)

input_image = imread({your filename})
height, width, channels = input_image.shape
bytesPerLine = channels * width
qImg = QtGui.QImage(input_image.data, width, height, bytesPerLine, QtGui.QImage.Format_RGB888)
pixmap01 = QtGui.QPixmap.fromImage(qImg)
pixmap_image = QtGui.QPixmap(pixmap01)
label_imageDisplay = QtWidgets.QLabel()
label_imageDisplay.setPixmap(pixmap_image)
label_imageDisplay.setAlignment(QtCore.Qt.AlignCenter)
label_imageDisplay.setScaledContents(True)
label_imageDisplay.setMinimumSize(1,1)
label_imageDisplay.show()
sys.exit(app.exec_())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.