PC屏幕颜色识别实现鼠标自动点击

实现自动化控制,需求:在电脑屏幕中两个点出现红色,然后用鼠标点击右下方刷新,实现代码如下,可是在实际运行中,运行一段时间就会失效(假死)不结束也不报错。请各位大佬帮忙看看能否有解决方案,谢谢!

from ctypes import windll
import win32api
import win32con
import time
from datetime import  datetime

def get_color1(x, y): #获取屏幕固定1像素rgb值
    list1 =[ ]
    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]

def mouse_buten(m,n):#鼠标点击
    width = windll.user32.GetSystemMetrics(0)  # 获取屏幕宽度
    height = windll.user32.GetSystemMetrics(1)  # 高度
    # print(width, height)
    windll.user32.SetCursorPos(m, n)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, m, n)  # 鼠标左键按下
    time.sleep(0.05)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, m, n)  #鼠标左键抬起0
    time.sleep(1)

h=0
var = 0
while var == 0:
    list1 = get_color1(1290, 57)
    list2 = get_color2(1250, 88)
    if (50 < list1[0] < 61 and 170 < list1[1] < 180 and 74 < list1[2] < 106) or \
            (60 < list2[0] < 61 and 60 < list2[1] < 65 and 64 < list2[2] < 66):
        mouse_buten(1220,757)
        now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        h += 1
        print(h, "刷新成功", now_time)
        time.sleep(3)
    else:
        time.sleep(0.5)
       

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值