PyQT5之网格布局多图片显示

选择文件夹,显示图片

import os

from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui
import sys
import cv2


class ButtonPanel(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.image_files = []
        self.image_labels = []
        self.img_idx = -1
        select_btn = QtWidgets.QPushButton("图像文件夹选择")
        # back_pix = QtWidgets.QStyle.SP_ArrowBack
        # back_icon = self.style().standardIcon(back_pix)     # 向后图标
        # back_btn = QtWidgets.QPushButton(back_icon, "")
        # back_btn.setToolTip("上一张...")                     # 鼠标悬停显示上一张
        # back_btn.setMinimumHeight(48)
        #
        # forward_pix = QtWidgets.QStyle.SP_ArrowForward
        # forward_icon = self.style().standardIcon(forward_pix)   # 向前图标
        # forward_btn = QtWidgets.QPushButton(forward_icon, "")
        # forward_btn.setToolTip("下一张...")                      # 鼠标悬停显示下一张
        # forward_btn.setMinimumHeight(48)

        self.path_label = QtWidgets.QLabel()
        self.path_label.setText("当前未显示图像路径")
        self.path_label.setAlignment(QtCore.Qt.AlignCenter)  # label上居中显示
        self.path_label.setMaximumHeight(50)  # label最大高度设置
        self.path_label.setStyleSheet("background-color:pink;color:green")  # 背景颜色设置
        font = QtGui.QFont()
        font.setBold(True)
        font.setPointSizeF(10)
        self.path_label.setFont(font)

        # self.image_label = QtWidgets.QLabel()

        # 方法二:使用cv2显示
        btn_panel = QtWidgets.QGroupBox("图像文件选择")
        hboxlayout = QtWidgets.QHBoxLayout()
        hboxlayout.addWidget(self.path_label)
        hboxlayout.addWidget(select_btn)
        hboxlayout.addStretch(1)
        btn_panel.setLayout(hboxlayout)
        
        grid_panel = QtWidgets.QGroupBox("网格显示")
        grid_layout = QtWidgets.QGridLayout()
        for i in range(9):
            image_label = QtWidgets.QLabel()
            image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
            image_label.setStyleSheet("background-color:blue;color:green")  # 背景颜色设置
            image_label.setFixedSize(256, 256)
            self.image_labels.append(image_label)
            grid_layout.addWidget(image_label, int(i / 3), int(i % 3))    # 行、列

        grid_panel.setLayout(grid_layout)

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(btn_panel)
        vboxlayout.addWidget(grid_panel)
        vboxlayout.addStretch(1)
        self.setLayout(vboxlayout)

        # 绑定点击
        select_btn.clicked.connect(self.on_select_image_dir)

    def on_select_image_dir(self):
        # 获取文件夹地址
        curr_dir = QtWidgets.QFileDialog.getExistingDirectory(self, "图像文件夹", ".")
        files = os.listdir(curr_dir)
        self.image_files.clear()
        self.path_label.setText(curr_dir)

        for ind in range(len(files)):
            print(files[ind])
            if ind >= 9:
                break
            pixmap = QtGui.QPixmap(os.path.join(curr_dir, files[ind]))
            pix = pixmap.scaled(QtCore.QSize(246, 246), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
            self.image_labels[ind].setPixmap(pix)   # 设置图像显示


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    main_win = QtWidgets.QMainWindow()
    main_win.setWindowTitle("图像网格化显示")
    myPanel = ButtonPanel()
    main_win.setCentralWidget(myPanel)
    main_win.setMinimumSize(1080, 720)
    main_win.show()
    app.exec_()

posted @ 2024-06-04 18:12  星空28  阅读(22)  评论(0编辑  收藏  举报