python - 使用标签显示图像

标签 python pyqt pyqt5 qimage qlabel

我已将一个 numpy 数组转换为一个像素图,以便在我的 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()

最佳答案

试试这个:

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

关于python - 使用标签显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42570214/

相关文章:

python - 突出显示 QTextEdit 文档上的行

python - PyQt5:使用事件绘制多个矩形

python - UI 关闭时 PyQT 应用程序挂起

python - PyQt5 从右到左编程

python - 为什么 Qt Designer 在 PyQt5 中创建额外不必要的浪费代码?

python - 子进程没有调用我的命令(或者做错了)

python - 特威通 : Filtering more than one track.

python - 删除主机名的公共(public)元素(缩短主机名)- DRY

python - PyCharm import matplotlib.pyplot 显示错误

python - 如何将 QTimeEdit 中的小时、分钟值显示到相应的 LCD 上?