python - 如何在pyqt5的Qlabel中显示图像
问题描述
我正在尝试使用标签在 pyqt5 中显示图像,但是当我单击按钮时,图像未显示。而gui正在接近。
这是我的代码
import sys
import cv2
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication ,QDialog
from PyQt5.uic import loadUi
from PyQt5.QtGui import QImage,QPixmap
from PyQt5 import QtCore
class anyname(QDialog):
def __init__(self):
super(anyname,self).__init__()
loadUi('image.ui',self)
self.SHOW.clicked.connect(self.onClicked)
self.TEXT.setText('kindly press show button')
@pyqtSlot()
def onClicked(self):
img= cv2.imread('E:/photoshopimages/images.png')
self.displayImage(img,1)
cv2.destroyAllwindows()
def displayImage(self, img,window=1):
qformat = QImage.Format_Indexed8
if len(img.shape) == 3:
if (img.shape[2]) == 4:
qformat=QImage.Format_RGBA888
else:
qformat=QImage.Format_RGB888
img =QImage(img, img.shape[1],img.shape[0],qformat)
img =img.rgbSwapped()
self.imgLabel.setPixmap(QPixmap.fromImage(img))
self.imgLabel.setAlignment(QtCore.Qt.AlignHCenter | Qtcore.Qt.AlignVCenter)
app=QApplication(sys.argv)
window=anyname()
window.show()
try:
sys.exit(app.exec_())
except:
print("exiting")
解决方案
推荐阅读
- ruby - 转发迭代器
- microsoft-graph-api - Microsoft Graph API - 获取组 - 权限错误
- c# - 不能在静态类中声明实例成员
- r - 当每天和一夜之间有多个开始和结束时,如何创建具有特定条件的开始和结束时间间隔
- reactjs - 反应图不可见并显示错误
- python - 设置用户友好和显式标志值的大多数 Pythonic 方式?
- mysql - 我无法在我的 docker compose 中进行 mysql php artisan 迁移。错误 SQLSTATE[HY000] [2002]
- javascript - 根据属性合并对象数组中的元素
- excel - 检查是否已设置工作簿变量
- excel - 突出显示两列中的重复单元格