吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 879|回复: 12
收起左侧

[Python 原创] 使用python实现一个取色小工具

[复制链接]
18834161486 发表于 2024-2-23 10:23
ui用的wxpython,可以自行
[Asm] 纯文本查看 复制代码
pip install wxpython
[Python] 纯文本查看 复制代码
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='坐标:(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)
        # 创建一个定时器来定期获取桌面颜色并更新标签
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
        self.timer.Start(1)  # 每隔1秒触发一次定时器

    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'坐标:({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图像转换为wxPython图像对象
        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)

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

jinmouyuzhao 发表于 2024-2-23 10:35
学习了,大佬厉害!
qtwer2 发表于 2024-2-23 10:48
 楼主| 18834161486 发表于 2024-2-23 11:06
冥界3大法王 发表于 2024-2-23 11:15
python for delphi 真的好强大。
liushuyan 发表于 2024-2-23 11:43
学习了,大佬厉害!
seawaycao 发表于 2024-2-23 12:17
学习了,大佬厉害!
sai609 发表于 2024-2-23 12:54
颜色编码明细表在哪
 楼主| 18834161486 发表于 2024-2-23 13:35
sai609 发表于 2024-2-23 12:54
颜色编码明细表在哪

getpixel可以直接获取对应坐标点的rgb值,然后十六进制转换就行了
wikiyc 发表于 2024-2-23 16:04
大佬牛 ,谢谢分享。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 16:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表