numpy 用于图像抓取的Bbox

m1m5dgzv  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(158)

所以,我想做一个自动化的应用程序
其实我是为Dino网络游戏做的
一切正常,但是!颜色数组的编号不变,我认为是装箱问题
您能指导我在此框中输入正确的值吗?

from PIL import ImageGrab, ImageOps
from webbrowser import open_new_tab as new
from pyautogui import keyDown
from time import sleep
from numpy import *

site_url = "https://trex-runner.com/"
dinasour = (692, 494)

def pressSpaceButton():
    sleep(0.007)
    keyDown('space')
    
      
def openGamePage(): # Open Game URL In New Tab
    new(site_url)

def restartGame(): # Press Space Button To Start/Restart Game
    keyDown('space')
    print("Game Has Been Started / Restarted")

def FindCactuses(): # Find Cactuses In Screen
    box = ( #(top_left_x, top_left_y, bottom_right_x, bottom_right_y)
        dinasour[0] + 30,
        dinasour[1],
        dinasour[0] + 120, 
        dinasour[1] + 2   
    )
    image = ImageGrab.grab(box)
    grayImage = ImageOps.grayscale(image)
    a = array(grayImage.getcolors())
    print(a)
    return a.sum()

    
sleep(3) # Wait 3 Seconds
openGamePage()

sleep(5)# Wait 5 Seconds
restartGame()

  
while True:  
    FindCactuses()
    if FindCactuses != 697:
        pressSpaceButton()

它将识别黑色和白色,当它找到黑色时,它将按空格键

owfi6suc

owfi6suc1#

如果你只想在某个x,y位置上匹配某个rgb值,那么你可以使用pyautogui.pixelMatchesColor(x, y, (r, g, b)),它非常适合这种情况。
因此,在您的代码中(请记住,您必须更改x,y值):

while True:
    #if at x 497 and y 524 the pixel matches your rgb color value of 83,83,83 then
    if pyautogui.pixelMatchesColor(497, 524,  (83, 83, 83)):
    #press up
    pyautogui.press('up')

相关问题