基于Python制作一款屏幕颜色提取器
作者:slandarer 发布时间:2023-11-16 05:20:48
标签:Python,屏幕,颜色,提取
1.使用效果
如下面动图所示,点击取色按钮后,将鼠标移动到想要取色的位置,等待两秒即可取色:
点击save color按钮可以将颜色存储到color library区域,同时如图所示,
! ! ! !红框内所有的方形元件其实都是按钮,点击后能获得颜色信息 ! ! ! !
信息格式为:(173, 189, 163) #ADBDA3 (48, 35, 189)
2.所需python包
PySide2
pyautogui
pyperclip
ctypes
from PySide2.QtWidgets import QApplication,QWidget,QLineEdit
from PySide2.QtWidgets import QLabel,QPushButton
from PySide2.QtGui import QIcon,QFont
from PySide2.QtCore import Qt
import sys
sys.path.append("...")
import time
import pyautogui as pag
import pyperclip
from ctypes import *
3.python小技巧
3.1控件数组
和matlab一样,python得控件也能构成数组,例如我在创建小颜色框时,是这么写的:
# 存储颜色框
for i in range(0,2):
for j in range(0,10):
storeColorBox=QPushButton(self)
storeColorBox.setGeometry((0.04+0.093*j)*Width,(0.475+0.07*i)*Height,0.08*Width,0.06*Height)
storeColorBox.setStyleSheet(self.SS_Color_Box)
storeColorBox.setProperty("storeId",i*10+j)
storeColorBox.setProperty("Color",'#FFFFFF')
storeColorBox.clicked.connect(self.selectedStore)
self.storeList.append(storeColorBox)
就是将各个颜色框存入了self.storeList这个数组,
想要例如想要调用第i个颜色框,就可以这么写:
storeBox=self.storeList[i]
3.2将控件作为属性
将控件控件作为属性赋给另一个控件
因为我们想要点击左侧标签时获得对应文本框内信息:
就可以这么写(举个例子):
# 创建文本框
self.CB1=QLineEdit(self)
self.CB1.setText("255,255,255")
self.CB1.move(0.62*Width,0.03*Height)
self.CB1.resize(0.35*Width,0.065*Height)
self.CB1.setFont(qf)
self.CB1.setStyleSheet(self.SS_Inf_Box)
# 创建标签
self.CL1=QPushButton(self)
self.CL1.setGeometry(0.448*Width,0.025*Height,0.14*Width,0.075*Height)
self.CL1.setStyleSheet(self.SS_Inf_Label)
self.CL1.setText("RGB")
self.CL1.setFont(qf)
self.CL1.setProperty('Children',self.CB1) # 把控件作为属性
self.CL1.clicked.connect(self.copyInf)# 与回调函数相连
然后我的回调函数是这样写的:
def copyInf(self):
infLabel=self.sender()
infBox=infLabel.property('Children') # 通过找到属性找到对应控件
pyperclip.copy(infBox.text())
3.3怎样重设控件颜色
还是通过setStyleSheet设置嗷,不过把字符串部分应该放颜色的部分换成了要重设的颜色:
self.mainBox.setStyleSheet("QPushButton{background:"+self.curColor_HEX
+";border: 3px solid rgb(150,150,150);border-radius:8px}")
3.4一些用到的字符串操作
字符串字母大写
STR=STR.upper()
字符串去掉空格
例如把(10, 50, 255)变为(10,50,255)
STR=STR.replace(" ", "")
字符串去掉两头括号
例如把(10,50,255)变为10,50,255
STR=STR[1:-1]
3.5鼠标位置像素颜色
如何获得当前鼠标位置像素颜色
import pyautogui as pag
from ctypes import *
x,y=pag.position()
RGB=get_color(x,y)
# 获取x,y位置像素颜色
def get_color(x, y):
gdi32 = windll.gdi32
user32 = windll.user32
hdc = user32.GetDC(None) # 获取颜色值
pixel = gdi32.GetPixel(hdc, x, y) # 提取RGB值
r = pixel & 0x0000ff
g = (pixel & 0x00ff00) >> 8
b = pixel >> 16
return [r, g, b]
3.6窗口始终置顶
self.setWindowFlags(Qt.WindowStaysOnTopHint)
3.7文本框不允许编辑但允许复制
用setFocusPolicy(Qt.NoFocus),例如程序中:
self.CB1.setFocusPolicy(Qt.NoFocus)
self.CB2.setFocusPolicy(Qt.NoFocus)
self.CB3.setFocusPolicy(Qt.NoFocus)
4.完整代码
from PySide2.QtWidgets import QApplication,QWidget,QLineEdit
from PySide2.QtWidgets import QLabel,QPushButton
from PySide2.QtGui import QIcon,QFont
from PySide2.QtCore import Qt
import sys
sys.path.append("...")
import time
import pyautogui as pag
import pyperclip
from ctypes import *
# ===========================================================================================
# 相关函数:
# 获取x,y位置像素颜色
def get_color(x, y):
gdi32 = windll.gdi32
user32 = windll.user32
hdc = user32.GetDC(None) # 获取颜色值
pixel = gdi32.GetPixel(hdc, x, y) # 提取RGB值
r = pixel & 0x0000ff
g = (pixel & 0x00ff00) >> 8
b = pixel >> 16
return [r, g, b]
# HEX转RGB
def hex2rgb(hexcolor):
'''HEX转RGB
:param hexcolor: int or str
:return: Tuple[int, int, int]
>>> hex2rgb(16777215)
(255, 255, 255)
>>> hex2rgb('0xffffff')
(255, 255, 255)
'''
hexcolor = int(hexcolor, base=16) if isinstance(hexcolor, str) else hexcolor
rgb = ((hexcolor >> 16) & 0xff, (hexcolor >> 8) & 0xff, hexcolor & 0xff)
return rgb
# RGB转HEX
def rgb2hex(r, g, b):
color = "#"
color += str(hex(r)).replace('x','0')[-2:]
color += str(hex(g)).replace('x','0')[-2:]
color += str(hex(b)).replace('x','0')[-2:]
return color
# RGB转HSV
def rgb2hsv(r, g, b):
r, g, b = r/255.0, g/255.0, b/255.0
mx = max(r, g, b)
mn = min(r, g, b)
m = mx-mn
if mx == mn:
h = 0
elif mx == r:
if g >= b:
h = ((g-b)/m)*60
else:
h = ((g-b)/m)*60 + 360
elif mx == g:
h = ((b-r)/m)*60 + 120
elif mx == b:
h = ((r-g)/m)*60 + 240
if mx == 0:
s = 0
else:
s = m/mx
v = mx
H = h / 2
S = s * 255.0
V = v * 255.0
return (round(H), round(S), round(V))
# ===========================================================================================
# 窗口类:
class Window(QWidget):
def __init__(self,Width=450,Height=600):
super().__init__()
self.setWindowTitle("getScreenColor")
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.Width=Width
self.Height=Height
# 样式表
self.SS_bkg_Label="QLabel{background: rgb(220,220,220);color:rgb(62,62,62);border-radius:8px}"
self.SS_Inf_Label="QPushButton{background: rgb(79,148,204);color:rgb(240,240,240);border-radius:8px}"
self.SS_Inf_Box="QLineEdit{border-radius:3px;border: 2px solid rgb(149,179,215);color:rgb(92,92,92)}"
self.SS_Main_Box="QPushButton{background: #FFFFFF;border: 3px solid rgb(150,150,150);border-radius:8px}"
self.SS_Color_Box="QPushButton{background: #FFFFFF;border: 2px solid rgb(150,150,150);border-radius:3px}"
self.SS_btn_1="QPushButton{background: rgb(214,219,233);color:rgb(82,82,82)}"
self.SS_btn_2="QPushButton{background: rgb(225,235,205);color:rgb(82,82,82)}"
self.SS_btn_3="QPushButton{background: rgb(232,191,190);color:rgb(82,82,82)}"
# 该类私有变量或属性
self.defaultColor=['#58827E','#144853','#4C6756','#849E77','#ADBDA3',
'#6B1B1E','#A94047','#E05E60','#F8A2AF','#E4CEDB',
'#B0A087','#7F877C','#C7C7BB','#D4C7BE','#E3E4DF',
'#C63866','#FE676E','#FD8F52','#FFBF73','#FFDCA2',
'#7292B8','#769EB8','#B4C5D7','#C5D5EC','#D9E0EA',
'#681F71','#7E0D5D','#6E57A5','#B589BE','#C993B7',
'#3978A4','#81AAAE','#EBCFC4','#FDB8A8','#E3929B','#7D7294']
self.curBoxId=0
self.curColor_RGB=[255,255,255]
self.curColor_HEX='#FFFFFF'
self.curColor_HSV=[0,0,255]
self.storeList=[]
self.defaultList=[]
# 框架构造函数调用
self.setSize()
self.partition()
self.setInfBox()
self.setMainBox()
self.setBtn()
self.setIcon()
self.setColorBox()
# ================================================================================================
# 颜色框回调函数部分:
def selectedMain(self):
tColor_HEX=self.curColor_HEX
tColor_RGB=hex2rgb('0x'+tColor_HEX[1:])
tColor_HSV=rgb2hsv(tColor_RGB[0], tColor_RGB[1], tColor_RGB[2])
pyperclip.copy(str(tColor_RGB)+' '+tColor_HEX+' '+str(tColor_HSV))
print(str(tColor_RGB)+' '+tColor_HEX+' '+str(tColor_HSV))
def selectedStore(self):
storeBox=self.sender()
tColor_HEX=storeBox.property("Color")
tColor_RGB=hex2rgb('0x'+tColor_HEX[1:])
tColor_HSV=rgb2hsv(tColor_RGB[0], tColor_RGB[1], tColor_RGB[2])
pyperclip.copy(str(tColor_RGB)+' '+tColor_HEX+' '+str(tColor_HSV))
print(str(tColor_RGB)+' '+tColor_HEX+' '+str(tColor_HSV))
def selectedDefault(self):
defaultBox=self.sender()
tNum=defaultBox.property("defaultId")
tColor_HEX=self.defaultColor[tNum]
tColor_RGB=hex2rgb('0x'+tColor_HEX[1:])
tColor_HSV=rgb2hsv(tColor_RGB[0], tColor_RGB[1], tColor_RGB[2])
pyperclip.copy(str(tColor_RGB)+' '+tColor_HEX+' '+str(tColor_HSV))
print(str(tColor_RGB)+' '+tColor_HEX+' '+str(tColor_HSV))
# ------------------------------------------------------------------------------------------------
# 颜色信息标签回调
def copyInf(self):
infLabel=self.sender()
infBox=infLabel.property('Children')
pyperclip.copy(infBox.text())
print(infBox.text())
# ------------------------------------------------------------------------------------------------
# 按钮回调函数部分:
def getColor(self):
time.sleep(2)
x,y=pag.position()
self.curColor_RGB=get_color(x,y)
self.curColor_HSV=rgb2hsv(self.curColor_RGB[0], self.curColor_RGB[1], self.curColor_RGB[2])
self.curColor_HEX=rgb2hex(self.curColor_RGB[0], self.curColor_RGB[1], self.curColor_RGB[2]).upper()
RGB_STR=str(self.curColor_RGB).replace(" ", "")[1:-1]
HSV_STR=str(self.curColor_HSV).replace(" ", "")[1:-1]
self.CB1.setText(RGB_STR)
self.CB2.setText(self.curColor_HEX)
self.CB3.setText(HSV_STR)
self.mainBox.setStyleSheet("QPushButton{background:"+self.curColor_HEX
+";border: 3px solid rgb(150,150,150);border-radius:8px}")
def saveColor(self):
if self.curBoxId<20:
tempBox=self.storeList[self.curBoxId]
tempBox.setProperty("Color",self.curColor_HEX)
tempBox.setStyleSheet("QPushButton{background:"+self.curColor_HEX
+";border: 2px solid rgb(150,150,150);border-radius:3px}")
self.curBoxId+=1
def deleteColor(self):
if self.curBoxId>0:
self.curBoxId-=1
tempBox=self.storeList[self.curBoxId]
tempBox.setProperty("Color",'#FFFFFF')
tempBox.setStyleSheet(self.SS_Color_Box)
# ================================================================================================
# 框架构造函数部分:
def setSize(self):# 调整框架大小
self.setGeometry(80,80,self.Width,self.Height)
self.setMaximumSize(self.Width,self.Height)
self.setMinimumSize(self.Width,self.Height)
def setIcon(self):# 设置图标
appIcon=QIcon("ICON.ico")
self.setWindowIcon(appIcon)
def partition(self):# 各部分划分
Width=self.Width
Height=self.Height
qf=QFont()
qf.setBold(True)
qf.setPointSize(12)
qf.setFamily("Cambria")
# --part1--当前颜色显示框背景-----
self.bkgLabel1=QLabel(self)
self.bkgLabel1.setGeometry(0.024*Width,0.015*Height,0.4*Width,0.3*Height)
self.bkgLabel1.setStyleSheet(self.SS_bkg_Label)
# --part2--当前颜色信息背景-----
self.bkgLabel2=QLabel(self)
self.bkgLabel2.setGeometry(0.448*Width,0.015*Height,0.528*Width,0.3*Height)
self.bkgLabel2.setStyleSheet("QLabel{background: rgb(235,235,235);border-radius:8px}")
# --part3--颜色存储库背景-----
self.bkgLabel3=QLabel(self)
self.bkgLabel3.setGeometry(0.024*Width,0.41*Height,0.952*Width,0.205*Height)
self.bkgLabel3.setStyleSheet(self.SS_bkg_Label)
self.bkgLabel3_title=QLabel(self)
self.bkgLabel3_title.setGeometry(0.038*Width,0.415*Height,0.4*Width,0.05*Height)
self.bkgLabel3_title.setStyleSheet(self.SS_bkg_Label)
self.bkgLabel3_title.setText("Color Library")
self.bkgLabel3_title.setFont(qf)
# --part4--预设颜色库背景-----
self.bkgLabel4=QLabel(self)
self.bkgLabel4.setGeometry(0.024*Width,0.63*Height,0.952*Width,0.355*Height)
self.bkgLabel4.setStyleSheet(self.SS_bkg_Label)
self.bkgLabel4_title=QLabel(self)
self.bkgLabel4_title.setGeometry(0.038*Width,0.635*Height,0.8*Width,0.05*Height)
self.bkgLabel4_title.setStyleSheet(self.SS_bkg_Label)
self.bkgLabel4_title.setText("Color Library(default)")
self.bkgLabel4_title.setFont(qf)
def setInfBox(self):# 设置信息显示框
Width=self.Width
Height=self.Height
# 字体设置
qf=QFont()
qf.setBold(True)
qf.setPointSize(12)
qf.setFamily("Cambria")
# 绘制颜色信息框
qf.setPointSize(10)
self.CB1=QLineEdit(self)
self.CB1.setText("255,255,255")
self.CB1.move(0.62*Width,0.03*Height)
self.CB1.resize(0.35*Width,0.065*Height)
self.CB1.setFont(qf)
self.CB1.setStyleSheet(self.SS_Inf_Box)
#
self.CB2=QLineEdit(self)
self.CB2.setText("#FFFFFF")
self.CB2.move(0.62*Width,0.13*Height)
self.CB2.resize(0.35*Width,0.065*Height)
self.CB2.setFont(qf)
self.CB2.setStyleSheet(self.SS_Inf_Box)
#
self.CB3=QLineEdit(self)
self.CB3.setText("0,0,255")
self.CB3.move(0.62*Width,0.23*Height)
self.CB3.resize(0.35*Width,0.065*Height)
self.CB3.setFont(qf)
self.CB3.setStyleSheet(self.SS_Inf_Box)
#
self.CB1.setFocusPolicy(Qt.NoFocus)
self.CB2.setFocusPolicy(Qt.NoFocus)
self.CB3.setFocusPolicy(Qt.NoFocus)
# 绘制颜色信息标签
self.CL1=QPushButton(self)
self.CL1.setGeometry(0.448*Width,0.025*Height,0.14*Width,0.075*Height)
self.CL1.setStyleSheet(self.SS_Inf_Label)
self.CL1.setText("RGB")
self.CL1.setFont(qf)
self.CL1.setProperty('Children',self.CB1)
self.CL1.clicked.connect(self.copyInf)
#
self.CL2=QPushButton(self)
self.CL2.setGeometry(0.448*Width,0.125*Height,0.14*Width,0.075*Height)
self.CL2.setStyleSheet(self.SS_Inf_Label)
self.CL2.setText("HEX")
self.CL2.setFont(qf)
self.CL2.setProperty('Children',self.CB2)
self.CL2.clicked.connect(self.copyInf)
#
self.CL3=QPushButton(self)
self.CL3.setGeometry(0.448*Width,0.225*Height,0.14*Width,0.075*Height)
self.CL3.setStyleSheet(self.SS_Inf_Label)
self.CL3.setText("HSV")
self.CL3.setFont(qf)
self.CL3.setProperty('Children',self.CB3)
self.CL3.clicked.connect(self.copyInf)
def setMainBox(self):# 设置其他label
Width=self.Width
Height=self.Height
# 左上角当前颜色显示框
self.mainBox=QPushButton(self)
self.mainBox.setGeometry(0.04*Width,0.025*Height,0.368*Width,0.28*Height)
self.mainBox.setStyleSheet(self.SS_Main_Box)
self.mainBox.clicked.connect(self.selectedMain)
def setBtn(self):# 设置按钮
Width=self.Width
Height=self.Height
# 按钮字体
qf=QFont()
qf.setBold(True)
qf.setPointSize(10)
qf.setFamily("Cambria")
# 获取颜色按钮
self.bnt1=QPushButton(self)
self.bnt1.setGeometry(0.024*Width,0.33*Height,0.4*Width,0.06*Height)
self.bnt1.setStyleSheet(self.SS_btn_1)
self.bnt1.setText("Get Screen Color")
self.bnt1.setFont(qf)
self.bnt1.clicked.connect(self.getColor)
# 保存颜色按钮
self.bnt2=QPushButton(self)
self.bnt2.setGeometry(0.444*Width,0.33*Height,0.26*Width,0.06*Height)
self.bnt2.setStyleSheet(self.SS_btn_1)
self.bnt2.setText("Save Color")
self.bnt2.setFont(qf)
self.bnt2.clicked.connect(self.saveColor)
# 删除颜色按钮
self.bnt3=QPushButton(self)
self.bnt3.setGeometry(0.724*Width,0.33*Height,0.26*Width,0.06*Height)
self.bnt3.setStyleSheet(self.SS_btn_3)
self.bnt3.setText("Delete Last")
self.bnt3.setFont(qf)
self.bnt3.clicked.connect(self.deleteColor)
def setColorBox(self):# 绘制存储颜色及预设颜色框
Width=self.Width
Height=self.Height
# 存储颜色框
for i in range(0,2):
for j in range(0,10):
storeColorBox=QPushButton(self)
storeColorBox.setGeometry((0.04+0.093*j)*Width,(0.475+0.07*i)*Height,0.08*Width,0.06*Height)
storeColorBox.setStyleSheet(self.SS_Color_Box)
storeColorBox.setProperty("storeId",i*10+j)
storeColorBox.setProperty("Color",'#FFFFFF')
storeColorBox.clicked.connect(self.selectedStore)
self.storeList.append(storeColorBox)
# 预设颜色框
for i in range(0,4):
for j in range(0,10):
if i*10+j<36:
defaultColorBox=QPushButton(self)
defaultColorBox.setGeometry((0.04+0.093*j)*Width,(0.7+0.07*i)*Height,0.08*Width,0.06*Height)
defaultColorBox.setStyleSheet("QPushButton{background: "
+self.defaultColor[i*10+j]+";border: 2px solid rgb(150,150,150);border-radius:3px}")
defaultColorBox.setProperty("defaultId",i*10+j)
defaultColorBox.clicked.connect(self.selectedDefault)
self.defaultList.append(storeColorBox)
# ===========================================================================================
# 函数调用:
myapp = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(myapp.exec_())
来源:https://blog.csdn.net/slandarer/article/details/120122161


猜你喜欢
- 方法组成模式方法里的所有语句都必须处在同一个归纳层次上无用的注释让代码自我表白标注为什么这样,而不是如何这样对方法表现进行描述等于重复表现这
- 首先,我的索引结构是酱紫的。
- 前言vue3 支持用 jsx 实现组件,摆脱了 vue 文件式的组件,不再需要额外的指令,写法非常接近 React,减少记忆负担。本文简单的
- 一 .概述SQL Server 将某些系统事件和用户定义事件记录到 SQL Server 错误日志和 Microsoft Windows 应
- kNN(k-nearest neighbor)是一种基本的分类与回归的算法。这里我们先只讨论分类中的kNN算法。k邻近算法的输入为实例的特征
- 编程是数据科学中不可或缺的技能,虽然创建脚本来执行基本功能很容易,但编写大规模可读性良好的代码需要更多的思考。关于PEP-8pycodest
- 一、ASP的平反想到ASP 很多人会说 “asp语言很蛋疼,不能面向对象,功能单一,很多东西实现不了” 等等诸如此类。 以上说法都是错误的,
- python 根据正则表达式提取指定的内容正则表达式是极其强大的,利用正则表达式来提取想要的内容是很方便的事。 下面演
- 在sql语句中,如果查找某个文本字段值为空的可以用select * from 表 where 字段=''但是如果
- 一 介绍Python上有一个非常著名的HTTP库——requests,相信大家都听说过,用过的人都说非常爽!现在requests库的作者又发
- 在日常的编程中,我经常需要标识存在于文本文档中的部件和结构,这些文档包括:日志文件、配置文件、定界的数据以及格式更自由的(但还是
- PyCharm 中在使用Turtle(海龟)库绘图体系时,代码明明是正确可以运行的,但是没有提示 ,代码出现黄色标记以及红色波浪线 ,经验不
- 处理下拉列表需要使用selenium中的工具类Select,常用方法如下:示例网站:http://sahitest.com/demo示例场景
- 查询所有数据库的总大小方法如下:mysql> use information_schema;mysql> select conc
- 本文实例讲述了PHP获取二叉树镜像的方法。分享给大家供大家参考,具体如下:问题操作给定的二叉树,将其变换为源二叉树的镜像。解决思路翻转二叉树
- 当我们重装系统,或者是重装数据库之后,当附加数据库之后,发现数据库里面 有用户名,但是在sqlserver里面没有 登陆名例如 先看&nb
- 一 安装oracle数据库成功之后,会显示相关的数据库信息: 全局数据库名:oracle 系统标识符(SID):oracle 服务器参数文件
- Python encode()方法encode() 方法为字符串类型(str)提供的方法,用于将 str 类型转换成 bytes 类型,这个
- print函数的功能是打印图窗或保存为特定文件格式。语法print(filename,formattype)print(filename,f
- 一、前言前几天在Python最强王者群有个叫【dcpeng】的粉丝问了一个关于Pandas中的问题,这里拿出来给大家分享下,一起学习。想问一