创建一个能够识别在 Python 中按“E”的正确时机的机器人

问题描述 投票:0回答:1

What needs to be identifiedRight time to press 'E' 我正在尝试制作一个按“E”键来自动化钓鱼游戏的机器人,在照片中我展示了按下它时的样子(它是红色的),我想知道如何识别这种颜色变化,以便机器人自动按“E”。

我尝试这样做,但它无法识别颜色。这是我正在测试的代码:

import cv2
import pyautogui
from time import sleep
from PIL import Image

fishing_image_rgb = fishing_image.convert("RGB")
pixel_fishing_image = fishing_image_rgb.getpixel((10,20))

print(pixel_fishing_image)

# Color in RGB
color = (89, 42, 42)

while True:
    ps = pyautogui.screenshot(region=(1267, 833, 300, 100))
    ps.save('PS.png')
    ps.show()

    # Look for the color in the image
    if ps is not None:
        if color in ps.getcolors():
            print("Color found")
            pyautogui.press('e')
        else:
            print("Couldn't find the color")
            sleep(0.1) 
    else:
        print("Unable to capture screen.")

我尝试了这段代码,但没有成功。我尝试了这段代码,但没有成功。我用准确的颜色进行了测试,打印屏幕可以工作,但无法识别颜色。

python opencv pyautogui
1个回答
0
投票

你可以这样做:

# x and y are the coordinates of the pixel in the screenshot you want to know the position of.
color = ps.getpixel((x,y))
if color[0] == 89 and color[1] == 42 and color[2] == 42:
    print("Color found!")
    pyautogui.press('e')

如果不起作用,可能是你的颜色不准确,所以尝试这样修改 if 语句:

if color[0] > 65 and color[0] < 75:
    if color[1] > 37 and color[1] < 47:
        if color[2] > 37 and color[2] < 47:
            pyautogui.press('e')
© www.soinside.com 2019 - 2024. All rights reserved.