首页 > 其他分享 >PyQT5之Checkbox

PyQT5之Checkbox

时间:2024-06-05 10:46:19浏览次数:23  
标签:Checkbox chkbox self cv2 PyQT5 label path image

"""
复选框
"""
import os.path
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)

        select_btn = QtWidgets.QPushButton("图像选择")

        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显示
        src = cv2.imread("./image/img1.png")  # BGR
        image = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)  # 将BGR转为RGB
        h, w, c = image.shape
        img = QtGui.QImage(image.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
        pixmap = QtGui.QPixmap(img)
        pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
        self.image_label.setPixmap(pix)  # 设置图像显示
        self.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
        self.image_label.setStyleSheet("background-color:blue;color:green")  # 背景颜色设置

        self.gray_rbtn = QtWidgets.QRadioButton("灰度")
        self.gauss_rbtn = QtWidgets.QRadioButton("高斯模糊")
        self.laps_rbtn = QtWidgets.QRadioButton("拉普拉斯")
        self.origin_rbtn = QtWidgets.QRadioButton("原图")
        self.origin_rbtn.setChecked(True)      # 默认选择原图

        rbtn_panel = QtWidgets.QGroupBox("单选框")
        hbox = QtWidgets.QHBoxLayout()
        hbox.addWidget(self.gray_rbtn)
        hbox.addWidget(self.gauss_rbtn)
        hbox.addWidget(self.laps_rbtn)
        hbox.addWidget(self.origin_rbtn)
        hbox.addStretch(1)
        rbtn_panel.setLayout(hbox)

        self.txt_chkbox = QtWidgets.QCheckBox("算法选择")
        self.wm_chkbox = QtWidgets.QCheckBox("添加水印")
        self.time_chkbox = QtWidgets.QCheckBox("执行时间")

        chkbox_panel = QtWidgets.QGroupBox("复选框")
        hbox2 = QtWidgets.QHBoxLayout()
        hbox2.addWidget(self.txt_chkbox)
        hbox2.addWidget(self.wm_chkbox)
        hbox2.addWidget(self.time_chkbox)
        hbox2.addStretch(1)
        chkbox_panel.setLayout(hbox2)

        btn_panel = QtWidgets.QGroupBox("图像文件选择")
        hboxlayout = QtWidgets.QHBoxLayout()
        hboxlayout.addWidget(self.path_label)
        hboxlayout.addWidget(select_btn)
        hboxlayout.addStretch(1)
        btn_panel.setLayout(hboxlayout)

        self.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
        self.image_label.setStyleSheet("background-color:pink;color:green")  # 背景颜色设置

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(self.image_label)
        vboxlayout.addWidget(btn_panel)
        vboxlayout.addWidget(rbtn_panel)
        vboxlayout.addWidget(chkbox_panel)
        vboxlayout.addStretch(1)
        self.setLayout(vboxlayout)

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

        self.gray_rbtn.toggled.connect(self.on_gray_image)
        self.gauss_rbtn.toggled.connect(self.on_blur_image)
        self.laps_rbtn.toggled.connect(self.on_laps_image)
        self.origin_rbtn.toggled.connect(self.on_origin_image)

        # self.txt_chkbox.toggled.connect(self.on_txt_chkbox)
        # self.wm_chkbox.toggled.connect(self.on_wm_chkbox)
        # self.time_chkbox.toggled.connect(self.on_time_chkbox)

    def on_gray_image(self):
        file_path = self.path_label.text()
        print(file_path)
        # file_path = 'D:/Work/ProjectItems/OpenCV+PyQT开发项目实例/image/img2.png'
        # file_path.split('/')[-1]
        file_path = os.path.join('.\\image', file_path.split('/')[-1])
        src = cv2.imread(file_path)                     # BGR

        start = cv2.getTickCount()
        gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)    # 将BGR转为gray
        end = cv2.getTickCount()

        if self.txt_chkbox.isChecked():
            cv2.putText(gray, "GRAY", (50, 100), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        if self.wm_chkbox.isChecked():
            cv2.putText(gray, "XingKong", (50, 200), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        if self.time_chkbox.isChecked():
            time = (end - start) * 1000 / cv2.getTickFrequency()
            cv2.putText(gray, "%.2f" % time, (50, 300), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        rbg = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)    # 将gray转为RGB
        h, w, c = src.shape
        img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
        pixmap = QtGui.QPixmap(img)
        pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
        self.image_label.setPixmap(pix)      # 设置图像显示
        # self.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
        # self.image_label.setStyleSheet("background-color:blue;color:green")  # 背景颜色设置

    def on_blur_image(self):
        file_path = self.path_label.text()
        file_path = os.path.join('.\\image', file_path.split('/')[-1])
        src = cv2.imread(file_path)   # BGR

        start = cv2.getTickCount()
        blur = cv2.GaussianBlur(src, (0, 0), 15)   # 将BGR转为gray
        end = cv2.getTickCount()

        if self.txt_chkbox.isChecked():
            cv2.putText(blur, "Laplais", (50, 100), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        if self.wm_chkbox.isChecked():
            cv2.putText(blur, "XingKong", (50, 200), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        if self.time_chkbox.isChecked():
            time = (end - start)*1000 / cv2.getTickFrequency()
            cv2.putText(blur, "%.2f" % time, (50, 300), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        rbg = cv2.cvtColor(blur, cv2.COLOR_BGR2RGB)    # 将BGR转为RGB
        h, w, c = src.shape
        img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
        pixmap = QtGui.QPixmap(img)
        pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
        self.image_label.setPixmap(pix)      # 设置图像显示
        # self.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
        # self.image_label.setStyleSheet("background-color:blue;color:green")  # 背景颜色设置

    def on_laps_image(self):
        file_path = self.path_label.text()
        file_path = os.path.join('.\\image', file_path.split('/')[-1])
        src = cv2.imread(file_path)   # BGR

        start = cv2.getTickCount()
        laps = cv2.Laplacian(src, -1)   # -1表示输入和输出保持一致
        end = cv2.getTickCount()

        if self.txt_chkbox.isChecked():
            cv2.putText(laps, "Laplais", (50, 100), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        if self.wm_chkbox.isChecked():
            cv2.putText(laps, "XingKong", (50, 200), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        if self.time_chkbox.isChecked():
            time = (end - start)*1000 / cv2.getTickFrequency()
            cv2.putText(laps, "%.2f" % time, (50, 300), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), 2)

        rbg = cv2.cvtColor(laps, cv2.COLOR_BGR2RGB)  # 将BGR转为RGB
        h, w, c = src.shape
        img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
        pixmap = QtGui.QPixmap(img)
        pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
        self.image_label.setPixmap(pix)      # 设置图像显示
        # self.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
        # self.image_label.setStyleSheet("background-color:blue;color:green")  # 背景颜色设置

    def on_origin_image(self):
        file_path = self.path_label.text()
        file_path = os.path.join('.\\image', file_path.split('/')[-1])
        src = cv2.imread(file_path)   # BGR
        rbg = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)    # 将BGR转为RGB
        h, w, c = src.shape
        img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
        pixmap = QtGui.QPixmap(img)
        pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
        self.image_label.setPixmap(pix)      # 设置图像显示
        # self.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
        # self.image_label.setStyleSheet("background-color:blue;color:green")  # 背景颜色设置

    def on_select_image(self):
        fileinfo = QtWidgets.QFileDialog.getOpenFileName(self, "打开图像文件", ".", "图像文件(*.jpg *.png)")
        fileName = fileinfo[0]
        if fileName != "":
            self.path_label.setText(fileName)
            pixmap = QtGui.QPixmap(fileName)
            pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
            self.image_label.setPixmap(pix)      # 设置图像显示

    # def on_txt_chkbox(self):
    #     pass
    #
    # def on_wm_chkbox(self):
    #     pass
    #
    # def on_time_chkbox(self):
    #     pass


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_()


标签:Checkbox,chkbox,self,cv2,PyQT5,label,path,image
From: https://www.cnblogs.com/jackchen28/p/18232339

相关文章

  • PyQT5之网格布局多图片显示
    选择文件夹,显示图片importosfromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysimportcv2classButtonPanel(QtWidgets.QWidget):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)self.image_files......
  • PyQT5之图片滚动
    图片滚动importosfromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysimportcv2classButtonPanel(QtWidgets.QWidget):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)self.image_files=[]......
  • PyQT5之QPushButton和布局
    fromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysimportcv2defclick_btn1():print("clickbutton1")txt_label.setText("我是按钮1点击之后")defclick_btn2():print("clickbutton2")defclick_......
  • Python--PyQT5图像
    fromPyQt5importQtCore,QtGuiimportsysimportcv2if__name__=='__main__':app=QtWidgets.QApplication(sys.argv)main_win=QtWidgets.QMainWindow()main_win.setWindowTitle("QLabel图像显示演示")image_label=QtWidge......
  • Python---PyQT5水平垂直布局
    fromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysimportcv2if__name__=='__main__':app=QtWidgets.QApplication(sys.argv)main_win=QtWidgets.QMainWindow()main_win.setWindowTitle("QLabel图像显示演示"......
  • Python---PyQT5之QPushButton
    fromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysimportcv2classButtonPanel(QtWidgets.QWidget):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)select_btn=QtWidgets.QPushButton("......
  • PyQT5之label
    fromPyQt5importQtWidgetsfromPyQt5importQtCore,QtGuiimportsysif__name__=='__main__':app=QtWidgets.QApplication(sys.argv)main_win=QtWidgets.QMainWindow()main_win.setWindowTitle("我的第一个PyQT5应用")labe......
  • 基于MCTS的三维四子棋AI模拟和基于PyQt5的应用交互界面
    '''三维四子棋是在三维空间率先四子连珠的游戏,在传统五子棋基本被研究透的情况下,三维四子棋增加了规则和难度,更加考验计算力、空间感、观察力和想象力。本模块实现三维四子棋的GUI。仅供学习和参考。BySoulCheungOnMay28thEmail:a1594983583@163.comQQ:1594983583'''......
  • python 离线按照PyQt5
    首先外网下载所需要的安装包1.新建虚拟环境python-mvenvQtTest2.安装PyQt5和PyQt5-tools这两个库pipinstallpyQt5-tool-ihttps://mirrors.aliyun.com/pypi/simple/pipinstallPyQt5-tool-ihttps://mirrors.aliyun.com/pypi/simple/3.导出requirements.txt这个文......
  • PyQt5 GUI编程(组件使用)
    一.简介PyQt5是一个用于创建图形用户界面(GUI)应用程序的Python绑定,它基于Qt库。PyQt5提供了大量的组件(也称为控件或部件),用于构建复杂的用户界面。以下是一些常用的PyQt5组件二.组件用法1.数字组件(QLCDNumber)classFileChooserApp(QWidget):def__init__(self):......