显示所选文件夹中的图像

Displaying images from the selected folder

我正在开发一个 GUI,它有两个名为 'Open Directory' 的按钮,使用户能够打开 his/her 选择的目录。请注意,此目录仅包含图像。选择目录后,list_of_images 中的第一张图像将显示在 window.

还有一个名为 'next' 的按钮。如果用户按下此按钮,则 list_of_images 中的下一张图像将显示在 window 上。

我的文件夹中有 3 张图像,这意味着我的 list_of_images = ['a.jpg', 'b.jpg', 'c.jpg']

现在我面临的问题是,当我按下按钮 next 时,会显示下一张图片,但当我再次按下它时,它应该会显示第三张图片,但它没有。代码中可能存在什么问题?

我会提供重现问题的代码。但是您需要更改文件夹或在您的计算机上创建一个名为 test_images 的文件夹。

# Import pyqt stuff
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QApplication

# Import DL stuff
import matplotlib.pyplot as plt

# Import the python script generated by the Qt-Designer
from gui_firstdraft import Ui_main_window

# Import miscellaneous
import sys
import os

class mainProgram(QtWidgets.QMainWindow, Ui_main_window):


    def __init__(self, parent=None):

        # Inherit from the aforementioned class and set up the gui
        super(mainProgram, self).__init__(parent)
        self.setupUi(self)


    def all_callbacks(self):

        # Open directory callback
        self.openDirectory_button.clicked.connect(self.open_directory_callback)

        # Next button callback
        self.next_button.clicked.connect(self.next_button_callback)



    def open_directory_callback(self):

        # Paths
        self._base_dir = os.getcwd()
        self._images_dir = os.path.join(self._base_dir, 'test_images')

        # Open a File Dialog and select the folder path
        dialog = QFileDialog()
        self._folder_path = dialog.getExistingDirectory(None, "Select Folder")

        # Get the list of images in the folder and read using matplotlib and print its shape
        self.list_of_images = os.listdir(self._folder_path)
        self.list_of_images = sorted(self.list_of_images)

        # Length of Images
        print('Number of Images in the selected folder: {}'.format(len(self.list_of_images)))
        input_img_raw_string = '{}\{}'.format(self._images_dir, self.list_of_images[0])

        # Show the first Image in the same window. (self.label comes from the Ui_main_window class)
        self.label.setPixmap(QtGui.QPixmap(input_img_raw_string))
        self.label.show()
        

    def next_button_callback(self):
        
        # Total Images in List
        total_images = len(self.list_of_images)

        if self.list_of_images:
            try:
                for img in self.list_of_images:
                    self.label.setPixmap(QtGui.QPixmap('{}\{}'.format(self._images_dir, img)))
                    self.label.show()
                    
            except ValueError as e:
                print('The selected folder does not contain any images')
    

                
def execute_pipeline():

    # Make an object of the class and execute it
    app = QApplication(sys.argv)

    # Make an object and call the functions
    annotationGui = mainProgram()
    annotationGui.all_callbacks()
    annotationGui.show()

    # Exit the window
    sys.exit(app.exec_())


if __name__ == "__main__":
    execute_pipeline()

我怀疑它与 next_button_callback 有关,但我不确定是什么问题。

如果函数是显示列表中的下一张图片,我看不出循环的目的。保留对显示的当前文件的索引的引用。在 open_directory_callback 中,这将设置为 0。在 next_button_callback 中,它会递增。

def open_directory_callback(self):

    # Paths
    self._base_dir = os.getcwd()
    self._images_dir = os.path.join(self._base_dir, 'test_images')

    # Open a File Dialog and select the folder path
    dialog = QFileDialog()
    self._folder_path = dialog.getExistingDirectory(None, "Select Folder")

    # Get the list of images in the folder and read using matplotlib and print its shape
    self.list_of_images = os.listdir(self._folder_path)
    self.list_of_images = sorted(self.list_of_images)

    # Length of Images
    print('Number of Images in the selected folder: {}'.format(len(self.list_of_images)))
    input_img_raw_string = '{}\{}'.format(self._images_dir, self.list_of_images[0])

    # Show the first Image in the same window. (self.label comes from the Ui_main_window class)
    self.label.setPixmap(QtGui.QPixmap(input_img_raw_string))
    self.label.show()
    
    <b>self.i = 0</b>
    

def next_button_callback(self):
    
    # Total Images in List
    total_images = len(self.list_of_images)

    if self.list_of_images:
        try:
            <b>self.i = (self.i + 1) % total_images
            img = self.list_of_images[self.i]</b>
            self.label.setPixmap(QtGui.QPixmap('{}\{}'.format(self._images_dir, img)))
            self.label.show()

        except ValueError as e:
            print('The selected folder does not contain any images')