Python:检测颜色,然后点击该颜色。

5
我正在尝试截取屏幕截图,检查截图中是否有特定颜色,如果找到该颜色,则单击该颜色。
问题在于颜色的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)的范围内?


你能更明确地指出问题所在并提出你的问题吗? - daniula
我该如何正确使用"s.convert("P", palette=Image.ADAPTIVE, colors=5)",以便将颜色范围限制在类似于(0, 255, 0)的范围内? - user3577821
1个回答

1

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

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

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

我知道这可能不是你想要的,但这是一个解决方法。 - user2961646
谢谢。这基本上就是我要找的东西了。现在我可以删除 s.convert 这一行,对吗? - user3577821
如果 s.getpixel(xy) == (range(0, 255), range(0, 255), range(0, 255)): break这样应该可以在任何颜色上中断,对吧?唉,这很具有挑战性。=( - user3577821
你可以删除 s.convert 这行代码,但是这会使得程序对所有可能的颜色都无法正常工作。 - user2961646
你需要这样说:“如果 s.getpixel(xy)[0] == range(0,255) 并且 s.getpixel(xy)[1] == range(0,255)”… 如果不行的话告诉我。 - user2961646
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接