pyqt中想选择图片放入label中,在单机事件触发函数,函数中使用QFileDialog.getOpenFileName方法时程序崩溃,窗口直接消失,程序结束运行,也不报错。
```python
import sys
import cv2
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QFileDialog, QMainWindow
import photo
from Mainwindow import Ui_MainWindow
from PyQt5 import QtWidgets
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.select.clicked.connect(self.select_clicked)
# self.surf.clicked.connect(self.surf_clicked)
# self.match.clicked.connect(self.match_clicked)
# self.save.clicked.connect(self.save_clicked)
self.labels = [self.labelimge1, self.labelimge2]
def select_clicked(self):
files, filetype = QFileDialog.getOpenFileNames(self, '打开多个图片', self.cwd,
"*.jpg, *.png, *.JPG, *.JPEG, All Files(*)",QFileDialog.DontUseNativeDialog)
for i in range(len(files)):
img = QtGui.QPixmap(files[i]).scaled(self.labels[i].width(), self.labels[i].height())
self.labels[i].setPixmap(img)
# def surf_clicked(self):
# qimg = self.labelsurf.pixmap()
# src = qimage2mat(qimg)
# newsrc = photo.surf_fun(src)
# pix = matqimage(newsrc)
# self.labelsurf.setPixmap(pix)
# self.labelsurf.setWordWrap(True)
# self.labelsurf.setScaledContents(True)
#
# def match_clicked(self):
# qimg = self.labelkey.pixmap()
# src = qimage2mat(qimg)
# newsrc = photo.match_fun(src)
# pix = matqimage(newsrc)
# self.labelmatch.setPixmap(pix)
# self.labelmatch.setWordWrap(True)
# self.labelmatch.setScaledContents(True)
#
# def save_clicked(self):
# # 提取Qlabel中的图片
# img = self.label_4.pixmap().toImage()
# fpath, ftype = QFileDialog.getSaveFileName(self.centralwidget, "保存图片", "d:\\", "*.jpg;;*.png;;All Files(*)")
# img.save(fpath)
def qimage2mat(qtpixmap): # qtpixmap转opencv
qimg = qtpixmap.toImage()
temp_shape = (qimg.height(), qimg.bytesPerLine() * 8 // qimg.depth())
temp_shape += (4,)
ptr = qimg.bits()
ptr.setsize(qimg.byteCount())
result = np.array(ptr, dtype=np.uint8).reshape(temp_shape)
result = result[..., :3]
return result
def matqimage(cvimg): # opencv转QImage
if cvimg.ndim == 2: # 单通道
height, width = cvimg.shape
cvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2RGB)
cvimg = QImage(cvimg.data, width, height, QImage.Format_RGB888)
pix = QPixmap.fromImage(cvimg)
return pix
else: # 多个通道
width = cvimg.shape[1]
height = cvimg.shape[0]
pixmap = QPixmap(width, height) # 根据已知的高度和宽度新建一个空的QPixmap,
qimg = pixmap.toImage() # 将pximap转换为QImage类型的qimg
for row in range(0, height):
for col in range(0, width):
b = cvimg[row, col, 0]
g = cvimg[row, col, 1]
r = cvimg[row, col, 2]
pix = qRgb(r, g, b)
qimg.setPixel(col, row, pix)
pix = QPixmap.fromImage(qimg)
return pix
def matqimage_guass(cvimg): # opencv转QImage #guass 专用####
if cvimg.ndim == 2: # 单通道
height, width = cvimg.shape
cvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2RGB)
cvimg = QImage(cvimg.data, width, height, QImage.Format_RGB888)
pix = QPixmap.fromImage(cvimg)
return pix
else: # 多个通道
width = cvimg.shape[1] # 获取图片宽度
height = cvimg.shape[0] # 获取图片高度
pixmap = QPixmap(width, height) # 根据已知的高度和宽度新建一个空的QPixmap,
qimg = pixmap.toImage() # 将pximap转换为QImage类型的qimg
for row in range(0, height):
for col in range(0, width):
b = int(cvimg[row, col, 0] * 255) # 高斯加噪归一化了 要*255
g = int(cvimg[row, col, 1] * 255)
r = int(cvimg[row, col, 2] * 255)
pix = qRgb(r, g, b)
qimg.setPixel(col, row, pix)
pix = QPixmap.fromImage(qimg)
return pix # 转换完成,返回
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())
```
不知道你这个问题是否已经解决, 如果还没有解决的话:QFileDialog::getOpenFileName(父亲,标题,默认路径,过滤文件)
返回值QString
//文件对话框 参数 1 父亲 参数2 标题 参数3 默认打开路径 参数4 过滤文件格式
//返回值是 选取的路径
QString str = QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\w1396\\Desktop","(*.txt)");
qDebug() << str;