pyqt5 图片像素点取色工具

main.py

import os

from PyQt5 import QtGui

from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog
import sys
from mainwindow import Ui_MainWindow


class MyWindow(QMainWindow, Ui_MainWindow):
    Base_dir = os.path.curdir
    imgpath = None

    def __init__(self):
        super(MyWindow, self).__init__()
        QtGui.QFontDatabase.addApplicationFont("2.otf")
        self.setupUi(self)
        self.menu.triggered[QAction].connect(self.processtrigger)
        self.setMouseTracking(True)
        self.actionopen.setShortcut("Ctrl+O")
        self.actionexit.setShortcut("Ctrl+E")

    def processtrigger(self, action: QAction):
        if action == self.actionopen:

            fileName_choose, filetype = QFileDialog.getOpenFileName(self,
                                                                    "选取文件", self.Base_dir
                                                                    ,  # 起始路径
                                                                    "Images (*.png *.jpg *.xpm)")  # 设置文件扩展名过滤,用双分号间隔All Files (*)

            if fileName_choose == "":
                print("\n取消选择")
                return

            print("\n你选择的文件为:")
            print(fileName_choose)
            self.centralwidget.setStyleSheet("background-image:url(%s);" % fileName_choose)

            self.imgpath = fileName_choose
            print("文件筛选器类型: ", filetype)

    def mouseMoveEvent(self, a0: QtGui.QMouseEvent) -> None:

        pos = self.centralwidget.mapFromParent(a0.pos())
        print(pos)
        pixmap = self.centralwidget.grab()
        img = pixmap.toImage()
        color = img.pixelColor(pos.x(), pos.y())
        print(color.name())
        tips = "位置在(%s,%s),颜色为:%s,%s,%s" % (pos.x(), pos.y(), color.red(),color.green(),color.blue())
        self.centralwidget.setToolTip(tips)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

mainwindow.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1280, 743)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setMinimumSize(QtCore.QSize(1280, 720))
        self.centralwidget.setMaximumSize(QtCore.QSize(1280, 720))
        self.centralwidget.setMouseTracking(True)
        self.centralwidget.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 0, 255), stop:0.166 rgba(255, 255, 0, 255), stop:0.333 rgba(0, 255, 0, 255), stop:0.5 rgba(0, 255, 255, 255), stop:0.666 rgba(0, 0, 255, 255), stop:0.833 rgba(255, 0, 255, 255), stop:1 rgba(255, 0, 0, 255));")
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1280, 23))
        self.menubar.setObjectName("menubar")
        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        MainWindow.setMenuBar(self.menubar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)
        self.actionopen = QtWidgets.QAction(MainWindow)
        self.actionopen.setObjectName("actionopen")
        self.actionopen.setStatusTip("选择文件")
        self.actionexit = QtWidgets.QAction(MainWindow)
        self.actionexit.setStatusTip("退出程序")
        self.actionexit.setObjectName("actionexit")
        self.menu.addAction(self.actionopen)
        self.menu.addAction(self.actionexit)
        self.menubar.addAction(self.menu.menuAction())

        self.retranslateUi(MainWindow)
        self.actionexit.triggered.connect(MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menu.setTitle(_translate("MainWindow", "菜单"))
        self.actionopen.setText(_translate("MainWindow", "open"))
        self.actionexit.setText(_translate("MainWindow", "exit"))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值