我会给你一个做什么的想法,首先你可以使用任何库来读取图像,我使用 OpenCV,因为我稍后会处理图像,在我的情况下,我读取了一个包含一组图像的文件夹,但是图像的概念是相同的。你必须做两件事:
首先:
将按钮与执行文件浏览器的方法相关联,在我的例子中,我使用了 TK 库提供的方法,但还有更多。
第二:
file_browser 提供的路径用于加载图像,然后将此图像关联到 GUI 的标签,方法在 view_image 方法中指定。
基本上就是这样,您必须阅读文档并将其适应您的应用程序。
import sys
import cv2
import select
import os
import pathlib
import shutil
from imutils import paths
import pickle
from PyQt5 import uic, QtWidgets, QtGui
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import Qt,QSize
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QIntValidator,QDoubleValidator
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMessageBox,QListWidgetItem
from tkinter import filedialog
from imutils import paths
from operator import itemgetter, attrgetter
qtCreatorFile = "GUI.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.get_path_image_button.clicked.connect(self.get_images_dataset)
self.view_image_button.clicked.connect(self.view_image)
self.path_image = ''
def get_images_dataset(self):
root = Tk()
root.withdraw()
root.folder_name = filedialog.askdirectory(
title = 'Choose the directory of the input files'
)
self.path_dataset =root.folder_name
if (root.folder_name):
self.textBrowser.setText(
'[INFO] Image directory path successfully uploaded'
)
else:
self.textBrowser.setText(
'[WARNING] No valid route selected'
)
root.destroy()
def view_image(self):
image_path = self.path_image
image = cv2.cvtColor(image_path, cv2.COLOR_BGR2RGB)
height, width, channel = image.shape
step = channel * width
qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
self.image_label.setPixmap(QPixmap.fromImage(qImg))
一个例子:
显然,这个结果与所附代码不对应,但它是一个例子。