Home > Blockchain >  Bbox For Image Grabbing
Bbox For Image Grabbing

Time:11-19

So , I'm Trying To Make A Automated App

Actually , i'm making it for Dino Web Game

Everything Is Fine , But ! Colors Array's Number Will Not Change , I Think It is Boxing Problem

Can You Guide Me With Correct Values In This Box ?

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

It's Going To Recognize Black And White Colors And When It Finds A Black Color it will press Space Button

CodePudding user response:

If you just want to pixel match a certain rgb value in a certain x,y position then you can use pyautogui.pixelMatchesColor(x, y, (r, g, b)) which is perfect for this situation

So in your code (keep in mind you will have to change the x,y values):

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')
  • Related