Python:检测颜色然后点击颜色

5 投票
1 回答
10555 浏览
提问于 2025-04-18 04:31

我想截个屏,然后检查这个截图里有没有某种颜色,如果找到了就点击它。

我遇到的问题是,颜色的RGB值必须完全一致。

我在想,是否可以把图片转换成颜色很少的那种图片。

抱歉让你们觉得杂乱。我其实没有受过专业训练,只是现在对编程特别着迷。

谢谢你们花时间来看这个。

import os, sys
import Image, ImageGrab, ImageOps
import time, random
from random import randrange
import win32api, win32con
from numpy import *


# Globals
# ------------------

x_pad = 0
y_pad = 0


# Screen Grab Function
def screenGrab():
    b1 = (x_pad + 1,y_pad+1,x_pad+1921,y_pad+1081)
    im = ImageGrab.grab()
    ##im.save(os.getcwd() + '\\Snap__' + str(int(time.time())) +'.png', 'PNG')
    return im

## Grab Mouse Position
## MousePos = win32api.GetCursorPos()
## print MousePos

## Type in shell to grab RGB color
## im = screenGrab()
## im.getpixel(MousePos)

## Check Mouse Position for Black
## s = screenGrab()
## if s.getpixel(MousePos) <= (96, 96, 96):
    ##print "I see black."   

# Main 

x = 920
y = 465

# Color Check Then Stop/Click Loop

while True:

    s = screenGrab()
    s.convert("P", palette=Image.ADAPTIVE, colors=5)
    x = x + 10
    xy = (x, y)
    if s.getpixel(xy)== (255, 255, 255):
        break
    else:
        win32api.SetCursorPos((x, y))
        print x
        print y

        if x == 1250:
            x = 700
            y = y + 10
            if y == 985:
                break

我该怎么正确使用 "s.convert("P", palette=Image.ADAPTIVE, colors=5)" 来限制颜色范围,比如限制到 (0, 255, 0) 这种颜色呢?

1 个回答

1

与其简化你的颜色,不如在寻找颜色的时候给出一系列的RGB值呢?

(range(200,220), range(200,220), range(200,220))

这样就可以避免改变所有像素的RGB值了。

撰写回答