【问题标题】:Bbox For Image Grabbing用于图像抓取的 Bbox
【发布时间】:2022-11-18 08:54:15
【问题描述】:

所以,我正在尝试制作一个自动化应用程序

实际上,我正在为 Dino Web Game 制作它

一切都很好,但是! Colors Array 的编号不会改变,我认为是装箱问题

你能用这个框中的正确值指导我吗?

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()

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

【问题讨论】:

    标签: python numpy python-imaging-library webbrowser-control pyautogui


    【解决方案1】:

    如果您只想在某个 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 then
        if pyautogui.pixelMatchesColor(497, 524,  (83, 83, 83)):
        #press up
        pyautogui.press('up')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多