I want to make a button where I can choose a image from a folder and afterwards display it in a window. I hope the picture helps to unterstand my problem.
I will give you an idea of what to do, first of all you can use any library to read images, I use OpenCV because I later process the images, in my case I read a folder with a set of images but the concept is the same for an image. You must do two things:
First: Associate a button to a method that executes a file browser, in my case I used the one provided by the TK library but there are many more.
Second: That path provided by the file_browser is used to load the image, then this image is associated to a label of the GUI the way to do it is specified in the view_image method.
That is basically the idea, you must read the documentation and adapt this to your application.
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))
An example:
Obviously, this result does not correspond to the attached code, but it serves as an example.