基于Python實(shí)現(xiàn)屏幕取色工具
1.簡介
屏幕取色小工具是一種實(shí)用的軟件工具,主要用于從屏幕上精確獲取顏色值,非常適合設(shè)計(jì)、編程等需要精確配色的領(lǐng)域。這類工具通常能夠從屏幕上任何區(qū)域精確提取顏色值,支持在整數(shù)值、RGB值、BGR值之間轉(zhuǎn)換。資源已打包成exe文件,大家需要可自行下載,喜歡請(qǐng)點(diǎn)個(gè)關(guān)注,主頁還有更多干貨資源!
2.運(yùn)行效果

3.相關(guān)源碼
from ctypes import windll
import cv2
from numpy import array as arr
from win32api import GetCursorPos, SetCursorPos
import wx
from PIL import ImageGrab
class colorData:
def __init__(self, pos=None, color=None, rgb=None):
self.pos = pos
self.color = color
self.rgb = rgb
class ColorFrame(wx.Dialog):
def __init__(self):
windll.user32.SetProcessDPIAware()
super().__init__(None, title='Desktop Color', size=(200, 300))
self.panel = wx.Panel(self)
self.zb = wx.StaticText(self.panel, label='坐標(biāo):(0, 0, 0)', style=wx.ALIGN_CENTER)
self.ys = wx.StaticText(self.panel, label='顏色:(0, 0, 0)', style=wx.ALIGN_CENTER)
self.RGB = wx.StaticText(self.panel, label='RGB:(0, 0, 0)', style=wx.ALIGN_CENTER)
self.bitmap = wx.StaticBitmap(self.panel, size=(200, 200))
self.data = colorData()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.zb, proportion=1, flag=wx.EXPAND)
sizer.Add(self.ys, proportion=1, flag=wx.EXPAND)
sizer.Add(self.RGB, proportion=1, flag=wx.EXPAND)
sizer.Add(self.bitmap, proportion=1, flag=wx.EXPAND | wx.ALL)
self.panel.SetSizer(sizer)
self.Bind(wx.EVT_CLOSE, self.on_close)
self.Bind(wx.EVT_CHAR_HOOK, self.on_key_press)
# 創(chuàng)建一個(gè)定時(shí)器來定期獲取桌面顏色并更新標(biāo)簽
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(1) # 每隔1秒觸發(fā)一次定時(shí)器
def on_timer(self, event):
point = GetCursorPos()
screenshot = ImageGrab.grab()
color = screenshot.getpixel(point)
img = arr(ImageGrab.grab((point[0] - 10, point[1] - 10, point[0] + 10, point[1] + 10)))
img = cv2.resize(img, None, None, fx=10, fy=10, interpolation=cv2.INTER_AREA)
cv2.rectangle(img, (100, 100), (110, 110), (255, 0, 0), 1)
self.update_label(point, color, img)
def update_label(self, point, color, img):
self.zb.SetLabel(f'坐標(biāo):({point[0]}, {point[1]})')
self.ys.SetLabel(f'顏色:({color[0]}, {color[1]}, {color[2]})')
self.RGB.SetLabel(f'RGB:({color[0]:02X}{color[1]:02X}{color[2]:02X})')
height, width, _ = img.shape
self.maps = wx.Bitmap.FromBuffer(width, height, img) # 將Opencv圖像轉(zhuǎn)換為wxPython圖像對(duì)象
self.bitmap.SetBitmap(self.maps)
def on_close(self, event):
self.timer.Stop()
self.Destroy()
def on_key_press(self, event):
keycode = event.GetKeyCode()
point = GetCursorPos()
if keycode == wx.WXK_RETURN or keycode == wx.WXK_NUMPAD_ENTER:
screenshot = ImageGrab.grab()
color = screenshot.getpixel(point)
self.data.pos = point
self.data.color = color
self.data.rgb = f'{color[0]:02X}{color[1]:02X}{color[2]:02X}'
self.on_close(event)
# self.EndModal(wx.ID_OK)
elif keycode == wx.WXK_LEFT:
SetCursorPos((point[0] - 1, point[1]))
elif keycode == wx.WXK_RIGHT:
SetCursorPos((point[0] + 1, point[1]))
elif keycode == wx.WXK_UP:
SetCursorPos((point[0], point[1] - 1))
elif keycode == wx.WXK_DOWN:
SetCursorPos((point[0], point[1] + 1))
def get_data(self):
return self.data
app = wx.App()
frame = ColorFrame()
frame.Show()
app.MainLoop()
print(frame.data.pos, frame.data.color, frame.data.rgb)
到此這篇關(guān)于基于Python實(shí)現(xiàn)屏幕取色工具的文章就介紹到這了,更多相關(guān)Python屏幕取色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?requests下載文件的幾種常用方法(附代碼)
這篇文章主要介紹了五種下載方式的實(shí)現(xiàn)方法,包括基礎(chǔ)下載、大文件分塊下載、帶有斷點(diǎn)續(xù)傳的下載、帶有超時(shí)和重試的下載以及完整的下載器實(shí)現(xiàn),文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2025-03-03
django 數(shù)據(jù)庫 get_or_create函數(shù)返回值是tuple的問題
這篇文章主要介紹了django 數(shù)據(jù)庫 get_or_create函數(shù)返回值是tuple的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
教你用Python代碼實(shí)現(xiàn)合并excel文件
近幾天一直因?yàn)閑xcel文件太多太雜的原因苦惱,今天特地整理了本篇文章,文章介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05

