简体   繁体   English

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

[英]Python : Detect Color Then Click Color

I am trying to take a screen shot, check the screen shot for a certain color, if the color is found then click on it. 我正在尝试进行屏幕截图,检查屏幕截图是否有某种颜色,如果找到该颜色,则单击它。

The problem I'm having is that the RGB values of the color has to be exact. 我遇到的问题是颜色的RGB值必须准确。

I was wondering if it was possible to convert the image to an image with very few colors. 我想知道是否有可能将图像转换为颜色很少的图像。

I'm sorry for the clutter. 对不起,我很混乱。 I am not properly trained. 我没有经过适当的培训。 I am just obsessed with coding now. 我现在沉迷于编码。

Thank you for taking the time to read this. 感谢您抽出时间来阅读。

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

How do I use "s.convert("P", palette=Image.ADAPTIVE, colors=5)" correctly so that I limit the range of colors to something like (0, 255, 0)? 我如何正确使用“ s.convert(“ P”,palette = Image.ADAPTIVE,colors = 5)”,以便将颜色范围限制为(0,255,0)?

Instead of simplifying your color why don't you give range of RGB values when trying to find what color it is? 除了简化颜色之外,为什么在查找颜色时也不给出RGB值范围?

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

This would work around changing the RGB values of all the pixels. 这可以解决更改所有像素的RGB值的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM