我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用pyautogui.locateOnScreen()。
def wait_for(image, runescape_window): # adding a possible failsafe in here time_entered = time.time() # could add a failsafe in here incase we misclick or something, this # should be something to come back to failsafe_count = 0 while(True): found = pyautogui.locateOnScreen(image, region=(runescape_window.top_left_corner[0], runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[ 0] - runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[1] - runescape_window.top_left_corner[1])) if found != None: break elif failsafe_count > 10: print("We can't seem to fix the problem so the script is now aborting") quit() elif time.time()-time_entered > 5 : failsafe_count += 1 print('We appear to be stuck so attempting to move the mouse and see if this fixes it') #print('For debug:') #print(runescape_window.bottom_right_corner[0], runescape_window.top_left_corner[0]) #print(runescape_window.bottom_right_corner[1], runescape_window.top_left_corner[1]) realmouse.move_mouse_to(random.randint(runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[0]), random.randint(runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[1])) #pyautogui.click() time_entered = time.time()
def cancel_offer(top_left_corner, bottom_right_corner, runescape_window): loc_of_ge_point = pointfrombox.random_point(top_left_corner, bottom_right_corner) realmouse.move_mouse_to(loc_of_ge_point[0], loc_of_ge_point[1]) pyautogui.click() # print('now waiting and then trying to locate x button') # wait_for('Tools/screenshots/abort_x_button.png', runescape_window) time.sleep(3+random.random()) fail_count = 0 # print('We are about to cancel an offer that we believe to be in the window with coords {}, we are at line 497'.format(runescape_window.bottom_right_corner)) while True: cancel_loc = pyautogui.locateOnScreen('Tools/screenshots/abort_x_button.png', region=(runescape_window.top_left_corner[0], runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[0]-runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[1]-runescape_window.top_left_corner[1])) if fail_count > 3: print('failed 3 times so trying to locate the abort button from the inventory collect button') to_inv_loc = pyautogui.locateOnScreen('Tools/screenshots/to_inv_button.png', region=(runescape_window.top_left_corner[0], runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[0]-runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[1]-runescape_window.top_left_corner[1])) cancel_loc = tuple(numpy.subtract(to_inv_loc, (163, 107, 163, 8))) if cancel_loc == None: print('Failed to locate the abort button {} times, trying again in 5 seconds'.format(fail_count)) time.sleep(5) fail_count += 1 else: break # print('we have saved the location of the x button ready to click') # print(cancel_loc) point_of_cancel_button = pointfrombox.random_point((cancel_loc[0], cancel_loc[1]), (cancel_loc[0]+cancel_loc[2], cancel_loc[1]+cancel_loc[3])) realmouse.move_mouse_to(point_of_cancel_button[0], point_of_cancel_button[1]) pyautogui.click()
def detect_money(top_left_corner, bottom_right_corner): global client_version money_icon_path = 'Tools/screenshots/money_icon_' + client_version + '.png' money_icon_loc = pyautogui.locateOnScreen(money_icon_path, region=(top_left_corner[0], top_left_corner[1], bottom_right_corner[0]-top_left_corner[0], bottom_right_corner[1] - top_left_corner[1])) money_val_loc = (money_icon_loc[0]+22, money_icon_loc[1], money_icon_loc[0]+100, money_icon_loc[1]+18) image = screengrab_as_numpy_array(money_val_loc) money_val = tesser_money_image(image) return(money_val)
def move_mouse_to_image_within_region(image, region): # region takes in an object image_loc = pyautogui.locateOnScreen(image, region=(region.top_left_corner[0], region.top_left_corner[1], region.bottom_right_corner[0]-region.top_left_corner[0], region.bottom_right_corner[1]-region.top_left_corner[1])) while(image_loc == None): image_loc = pyautogui.locateOnScreen(image, region=(region.top_left_corner[0], region.top_left_corner[1], region.bottom_right_corner[0]-region.top_left_corner[0], region.bottom_right_corner[1]-region.top_left_corner[1])) point_to_click = pointfrombox.random_point((image_loc[0], image_loc[1]), (image_loc[0]+image_loc[2], image_loc[1]+image_loc[3])) realmouse.move_mouse_to(point_to_click[0], point_to_click[1])
def loc_image(): print(pyautogui.locateOnScreen('Tools/screenshots/grand_exchange_button.png'))
def open_firefox(): # Printing basic message msg(1,'Opening Firefox...') # Location the start button _start_button_=pyautogui.locateOnScreen('images/start_button.png') _location_=pyautogui.center(_start_button_) # Clicking the start button if not pyautogui.click(_location_): msg(1,'Opened start menu successfully!') else: msg(3,'Failed to open start menu!') ext() time.sleep(1) # Search for Firefox in the menu search pyautogui.typewrite('firefox') pyautogui.typewrite('\n') # Print message msg(1,'Firefox is now open and running.') # Function used to locate GMail
def locate_gmail(): #Sleep for a while and wait for Firefox to open time.sleep(2) # Printing message msg(1,'Opening Gmail...') # Typing the website on the browser pyautogui.keyDown('ctrlleft'); pyautogui.typewrite('a'); pyautogui.keyUp('ctrlleft') pyautogui.typewrite('https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default') pyautogui.typewrite('\n') # Wait for a while until the website responds time.sleep(3) # Print a simple message msg(1,'Locating the form...') # Locate the form _gmail_=pyautogui.locateOnScreen('images/gmail_form.png') _form_=pyautogui.center(_gmail_) # Check and print message if not pyautogui.click(_form_): msg(1,'Located the form.') else: msg(3,'Failed to locate the form.') ext() # Function used to randomize credentials
def spec_printscreen(): # identify the top-right corner, doesnt find it!! # =========================================================================== # region_topright = pyautogui.locateOnScreen('topright.png')#,grayscale=True) # if region_topright is None: # print('Could not find topright picture') # else: # print(region_topright) # =========================================================================== # identify thedownleft corner from home button region_downleft = pyautogui.locateOnScreen('downleft.png') if region_downleft is None: print('Could not find downleft picture') else: # print(region_downleft) pass fig = plt.gcf() size = fig.get_size_inches() * fig.dpi # size in pixels # print(size) # im=pyautogui.screenshot(region=(0,0, 300, 400)) pyautogui.screenshot() im2 = pyautogui.screenshot('latest_screenshot.png', region=(region_downleft[0], region_downleft[1] - size[1], size[0], size[1])) ctypes.windll.user32.MessageBoxA(0, "spec print screenshot finished, find it in latest_screenshot.png", "Screenshot of the plot area", 0)
def wait_for_pull(cls): # we can give hero a tp scroll and use image to know when reload logger.info("Waiting for double pull to occur") time.sleep(13) # TODO this is terrible # while not pa.locateOnScreen(os.getcwd() + '\\button_images\\115.png'): # time.sleep(0.001) # else: # logger.info("Found sgnifier. Pull over. Results sent") # return
def sell_items(runescape_window, ge_slot, record_number_selling=False): # click correct sell bag move_mouse_to_image_within_region('Tools/screenshots/sell_bag.png', ge_slot) pyautogui.click() wait_for('Tools/screenshots/quantity_box.png', runescape_window) # click item in inv coords_of_item = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-180, runescape_window.bottom_right_corner[1]-372), (runescape_window.bottom_right_corner[0]-166, runescape_window.bottom_right_corner[1]-349)) realmouse.move_mouse_to(coords_of_item[0], coords_of_item[1]) pyautogui.click() # click all button incase move_mouse_to_image_within_region("Tools/screenshots/all_button.png", runescape_window) pyautogui.click() # recording number selling if needed if record_number_selling: try: time.sleep(1+random.random()) loc_of_all_button = pyautogui.locateOnScreen('Tools/screenshots/All_button.png', region=(runescape_window.top_left_corner[0], runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[0]-runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[1]-runescape_window.top_left_corner[1])) number_selling_image = screengrab_as_numpy_array((loc_of_all_button[0]-100,loc_of_all_button[1]-27,loc_of_all_button[0]-3,loc_of_all_button[1]-12)) quantity = tesser_quantity_image(number_selling_image) runescape_window.update_money(runescape_window.money+((ge_slot.item.quantity_to_buy-quantity)*ge_slot.item.price_instant_sold_at)) print('About to update the quantity to buy to {}'.format(quantity)) ge_slot.item.set_quantity_to_buy(quantity) except: print("Couldn't read the quantity bought correctly so setting score to invalid to prevent artificial high scores, money for the window may now be wrong too, we think there is {}gp in this window available".format(runescape_window.money)) ge_slot.item.set_score_invalid() # click price button coords_of_price_box = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-384, runescape_window.bottom_right_corner[1]-272), (runescape_window.bottom_right_corner[0]-291, runescape_window.bottom_right_corner[1]-259)) realmouse.move_mouse_to(coords_of_price_box[0], coords_of_price_box[1]) pyautogui.click() time.sleep(2+random.random()) # type price in and hit enter random_typer(str(ge_slot.item.price_instant_bought_at)) pyautogui.press('enter') # click confirm move_mouse_to_image_within_region("Tools/screenshots/confirm_offer_button.png", runescape_window) pyautogui.click() # update state of ge slot ge_slot.update_buy_or_sell_state('sell') wait_for('Tools/screenshots/lent_item_box.png', runescape_window) runescape_window.set_time_of_last_action() print('Placed a sell order for {} {} at {} each'.format(ge_slot.item.quantity_to_buy, ge_slot.item.item_name, ge_slot.item.price_instant_bought_at)) time.sleep(2+random.random()) ge_slot.set_image_of_slot()