displaying image using a label

此生再无相见时 提交于 2019-12-01 09:02:38

问题


I have converted a numpy array into a pixmap to display it on a label within my GUI. When I run the program, the GUI closes for some reason (no error messages).

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()

回答1:


Try with this:

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_())



来源:https://stackoverflow.com/questions/42570214/displaying-image-using-a-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!