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

use*_*821 5 python colors

我正在尝试拍摄屏幕截图,检查屏幕截图是否有某种颜色,如果找到颜色,则单击它。

我遇到的问题是颜色的 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
Run Code Online (Sandbox Code Playgroud)

如何正确使用 "s.convert("P", Palette=Image.ADAPTIVE, colors=5)" 以便将颜色范围限制为 (0, 255, 0) 之类的内容?

小智 1

为什么在尝试查找颜色时不给出 RGB 值的范围,而不是简化颜色?

(range(200,220), range(200,220), range(200,220))
Run Code Online (Sandbox Code Playgroud)

这可以解决更改所有像素的 RGB 值的问题。