基于Python制作一款屏幕顏色提取器
1.使用效果
如下面動圖所示,點(diǎn)擊取色按鈕后,將鼠標(biāo)移動到想要取色的位置,等待兩秒即可取色:
點(diǎn)擊save color按鈕可以將顏色存儲到color library區(qū)域,同時如圖所示,
! ! ! !紅框內(nèi)所有的方形元件其實(shí)都是按鈕,點(diǎn)擊后能獲得顏色信息 ! ! ! !
信息格式為:(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控件數(shù)組
和matlab一樣,python得控件也能構(gòu)成數(shù)組,例如我在創(chuàng)建小顏色框時,是這么寫的:
# 存儲顏色框 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這個數(shù)組,
想要例如想要調(diào)用第i個顏色框,就可以這么寫:
storeBox=self.storeList[i]
3.2將控件作為屬性
將控件控件作為屬性賦給另一個控件
因?yàn)槲覀兿胍c(diǎn)擊左側(cè)標(biāo)簽時獲得對應(yīng)文本框內(nèi)信息:
就可以這么寫(舉個例子):
# 創(chuàng)建文本框 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) # 創(chuàng)建標(biāo)簽 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)# 與回調(diào)函數(shù)相連
然后我的回調(diào)函數(shù)是這樣寫的:
def copyInf(self): infLabel=self.sender() infBox=infLabel.property('Children') # 通過找到屬性找到對應(yīng)控件 pyperclip.copy(infBox.text())
3.3怎樣重設(shè)控件顏色
還是通過setStyleSheet設(shè)置嗷,不過把字符串部分應(yīng)該放顏色的部分換成了要重設(shè)的顏色:
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)變?yōu)?10,50,255)
STR=STR.replace(" ", "")
字符串去掉兩頭括號
例如把(10,50,255)變?yōu)?0,50,255
STR=STR[1:-1]
3.5鼠標(biāo)位置像素顏色
如何獲得當(dāng)前鼠標(biāo)位置像素顏色
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文本框不允許編輯但允許復(fù)制
用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 * # =========================================================================================== # 相關(guān)函數(shù): # 獲取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轉(zhuǎn)RGB def hex2rgb(hexcolor): '''HEX轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)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)}" # 該類私有變量或?qū)傩? 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=[] # 框架構(gòu)造函數(shù)調(diào)用 self.setSize() self.partition() self.setInfBox() self.setMainBox() self.setBtn() self.setIcon() self.setColorBox() # ================================================================================================ # 顏色框回調(diào)函數(shù)部分: 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)) # ------------------------------------------------------------------------------------------------ # 顏色信息標(biāo)簽回調(diào) def copyInf(self): infLabel=self.sender() infBox=infLabel.property('Children') pyperclip.copy(infBox.text()) print(infBox.text()) # ------------------------------------------------------------------------------------------------ # 按鈕回調(diào)函數(shù)部分: 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) # ================================================================================================ # 框架構(gòu)造函數(shù)部分: def setSize(self):# 調(diào)整框架大小 self.setGeometry(80,80,self.Width,self.Height) self.setMaximumSize(self.Width,self.Height) self.setMinimumSize(self.Width,self.Height) def setIcon(self):# 設(shè)置圖標(biāo) 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--當(dāng)前顏色顯示框背景----- 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--當(dāng)前顏色信息背景----- 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--預(yù)設(shè)顏色庫背景----- 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):# 設(shè)置信息顯示框 Width=self.Width Height=self.Height # 字體設(shè)置 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) # 繪制顏色信息標(biāo)簽 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):# 設(shè)置其他label Width=self.Width Height=self.Height # 左上角當(dāng)前顏色顯示框 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):# 設(shè)置按鈕 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):# 繪制存儲顏色及預(yù)設(shè)顏色框 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) # 預(yù)設(shè)顏色框 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) # =========================================================================================== # 函數(shù)調(diào)用: myapp = QApplication(sys.argv) window = Window() window.show() sys.exit(myapp.exec_())
以上就是基于Python制作一款屏幕顏色提取器的詳細(xì)內(nèi)容,更多關(guān)于Python屏幕顏色提取器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python matplotlib 在指定的兩個點(diǎn)之間連線方法
今天小編就為大家分享一篇python matplotlib 在指定的兩個點(diǎn)之間連線方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05詳解python使用Nginx和uWSGI來運(yùn)行Python應(yīng)用
這篇文章主要介紹了詳解python使用Nginx和uWSGI來運(yùn)行Python應(yīng)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Python進(jìn)階之高級用法詳細(xì)總結(jié)
今天帶各位小伙伴學(xué)習(xí)一下Python高級語法,主要有Lambda表達(dá)式,map函數(shù),filter函數(shù),reduce函數(shù),三大推導(dǎo)式等,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-05-05python中enumerate函數(shù)用法實(shí)例分析
這篇文章主要介紹了python中enumerate函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了enumerate函數(shù)的功能、定義及使用技巧,需要的朋友可以參考下2015-05-05