def locateInWindow(self, image, region=None, entireScreen=False): wX, wY, wW, wH = _rect2xywh(self._getVamRect()) # If a region was supplied then convert the window-relative coordinates to screen coordinates if not region is None: print("Searching in region {}".format(region)) rX, rY, rW, rH = region wX += rX wY += rY wW = min(rW, wW - rX) wH = min(rH, wH - rY) windowRegion = (wX, wY, wW, wH) ret = pyautogui.locate( image, pyautogui.screenshot("haystack.png", region=windowRegion)) # Found a result in specified region. Convert to window coordinates if ret and region: retX, retY, retW, retH = ret ret = (retX + rX, retY + rY, retW + rW, retH + rH) # If not found in region then search entire screen if ret is None and entireScreen: print("Switching to entire screen") return self.locateInWindow(image) print("Returning: {}".format(ret)) return ret
def find_image_coordinates(img_name, left=0, up=0, right=0, down=0, type_of_conversion="center"): # Move coords 10 pixels left. (Not the usual left,top,right,down) # Take screenshot full_screenshot() # Icon/Image you want to find (the needle) needle_img = os.path.abspath("images/" + img_name) # Search desktop for a match (the haystack) haystack_img = os.path.abspath("images/" + Images.desktopWindow) # Locate image on desktop img_coordinates = pyautogui.locate(needle_img, haystack_img, grayscale=False) if img_coordinates is not None: if type_of_conversion is "center": # Locate image # If image found, get coordinates. img_coordinates_x, img_coordinates_y = pyautogui.center(img_coordinates) # See if we need to move right or left of the image center img_coordinates_x = img_coordinates_x / 2 # divide by half because mac retina screens are 2x resolution img_coordinates_y = img_coordinates_y / 2 if left != 0: img_coordinates_x = img_coordinates_x - left if up != 0: img_coordinates_y = img_coordinates_y + up if right != 0: img_coordinates_x = img_coordinates_x + right if down != 0: img_coordinates_y = img_coordinates_y - down return img_coordinates_x, img_coordinates_y elif type_of_conversion is "dimensionsConverted": img_coordinates_x, img_coordinates_y, width, height = img_coordinates return img_coordinates_x / 2, img_coordinates_y / 2, width / 2, height / 2 elif type_of_conversion is "dimensionsOriginal": return img_coordinates else: print("Image not found: " + img_name) return None
def get_bat_pos(self): self.get_bat_screenshot() try: bat = pag.locate('bat.png', "sc_bat.png", confidence=0.8) except: return self.bat_pos = pag.center(bat)
def get_troop_status(troop_slot): ts_images = { 'gathering': 'ts_gathering.png', 'back': 'ts_back.png', 'enemy_atk': 'ts_enemy_atk.png', 'monster_atk': 'ts_monster_atk.png', 'scouting': 'ts_scouting.png', 'transfer': 'ts_transfer.png', 'reinforce': 'ts_reinforce.png', 'rally': 'ts_rally.png' } result = [] go_kingdom() screenshot = adb.screenshot() for i in range(troop_slot): haystack = screenshot.crop(coords.troop_info_area[i]) for status, img in ts_images.items(): try: im = PIL.Image.open(img_path(img)) if pyautogui.locate(im, haystack, confidence=IMG_MATCH_CONFIDENCE): result.append(status) break except IOError: log('File is missing:', img_path(img)) log('get_troop_status:', result) return result
def click_buttons(button): window = scanwindow() location = auto.locate(r'IMG\%s.png' % button, window, confidence=0.6) auto.click(main_x + auto.center(location)[0], main_y + auto.center(location)[1], duration=0.5, pause=1.2)
def locate_in_image(needle, haystack, confidence=0.95): if not wc.is_window_foreground(): print('waiting for window to be foreground for image search...') wc.wait_for_window_foreground() return pag.locate( needle, haystack, confidence=confidence, region=gwg.get_game_window_rect())
def check_blue(each_im): signal = 1 im = Image.open(each_im) imp = im.load() x_i, y_i = im.size for i in range(x_i): for j in range(y_i): if imp[i, j] == (255, 255, 255): imp[i, j] = (0, 0, 0) elif imp[i, j] == (0, 0, 128): imp[i, j] = (255, 255, 255) im_c = im.crop((int(x_i / 2), 0, x_i, y_i)) im_tar = glob.glob(".\\white_image_dir\\*.png") for e_im in im_tar: im_t = Image.open(e_im) if pyautogui.locate(im_c, im_t): signal = 0 print("发现重复图片:" + str(each_im)) break if signal: im.save(".\\blue_to_white_temp\\" + each_im.split(os.sep)[-1]) print(str(each_im) + "未发现与现有图片重复,已成功转存。")
def config(): Loot = data['Loot'] useless, useless, width, height = clientRect() cap = screenshot(title=tibia, left=8, top=30, width=width, height=height, cleaning=False) image = Image.fromarray(cap) #image.save('config.png') useless, y, useless, useless = locate('afk.dat', image) top = 35 center = abs(top - y) if Loot == 'Far': left = int(width / 2 - center / 2) width = center height = center return (left, top, width, height) if Loot == 'Near': left = int((width - 210) / 2) top = int((center - 130) / 2) return (left, top, 230, 220)
def start_playing(num): white_point = False while not white_point: image = np.array(ImageGrab.grab(bbox=ball)) if image[0][0][0] > 200: print("Found Bowler") break img = cv2.cvtColor(np.array(ImageGrab.grab(bbox=player)), cv2.COLOR_BGR2RGB) rh = cv2.imread("RH.png") player_cords = pyautogui.locate(rh, img, confidence=0.7) right_hand = True if player_cords is not None else False find = False while not find: click_key_s = np.array(ImageGrab.grab(bbox=cord_S)) find = find_sphere(image=click_key_s, key_area='s') if find: keys = figure_keys(click_key_s, right_hand, 's') timing(timer, keys) time.sleep(2) break click_key_w = np.array(ImageGrab.grab(bbox=cord_W)) find = find_sphere(image=click_key_w, key_area='w') if find: keys = figure_keys(click_key_w, right_hand, 'w') timing(timer, keys) time.sleep(2) break
def get_single_batShot(self): self.take_screenshot("sc_bat.png") try: bat = pag.locate('bat.png', "sc_bat.png", confidence=0.8) except: return self.bat_pos = pag.center(bat)
def search(self, table_img): #print(self.relevant_box) if (self.relevant_box == 'ALL'): searched_img = table_img else: searched_img = table_img.crop( (self.relevant_box.left, self.relevant_box.top, self.relevant_box.left + self.relevant_box.width, self.relevant_box.top + self.relevant_box.height)) #print(box_relative) #searched_img.show() try: #Attempt to locate screen item self.box = pyautogui.locate(self.image_file_path, searched_img, confidence=self.detection_confidence) if (self.relevant_box != 'ALL'): self.box = Box(self.box.left + self.relevant_box.left, self.box.top + self.relevant_box.top, self.box.width, self.box.height) if (self.box != None): spotted = True else: spotted = False except: spotted = False #print('ScreenItem : "'+ self.id +'" is NOT available') return spotted
def findObject(needle, region='mainWindow', gray=True, confidence=0.9): ''' method to locate an object in a specific window using pyAutoGui string needle = object image name region = region to search in bool gray = use or not use grayscale confidence = percentage of location precision ''' region = getRegion(region) haystackImage = pygui.screenshot(region=region) result = pygui.locate('data/img/{}.png'.format(needle), haystackImage, grayscale=gray, confidence=confidence) location = None if result is not None: relativeLocation = pygui.center(result) location = (relativeLocation[0] + region[0], relativeLocation[1] + region[1]) if location is None: return None else: x, y = location return (x, y)
def imageRecognition(self, base_image: str, image: str, all_results: bool = False, grayscale: bool = False): eval(self.__action) if not os.path.isfile(image): self.__Exception( 'Base image do not exist ({base})'.format(base=base_image)) elif not os.path.isfile(image): self.__Exception( 'Image do not exist ({image})'.format(image=image)) else: if not all_results: position = pyautogui.locate(image, base_image, grayscale=grayscale) position = [position[0], position[1] ] if position is not None else None else: position = [] positions = pyautogui.locateAll(image, base_image, grayscale=grayscale) for i in positions: position.append([i[0], i[1]]) position = position if len(position) > 0 else None if position is None: self.__Exception( 'Image not found on base image'.format(image=image)) return None return position
def checkRod(): shutdown = False #open inventory if (invOpen() == False): pressI() time.sleep(2) #move out of the way of inv(right on pearl tab) pyautogui.moveTo(1600, 260, .4) bustedHeld = pyautogui.locateOnScreen('broke_rod.png', region=(1136, 549, 44, 44)) if(bustedHeld == None): pressI() time.sleep(.4) return else: inventoryRegion = pyautogui.screenshot(region=(1463,328,423,423)) for col in range(0,8): for row in range(0,8): rod_search = pyautogui.locate('eph_10_rod.png', inventoryRegion, region=( (row*45)+(row*8)+row, (col*45)+(col*8)+col, 45, 45) ) if(rod_search == None): #os.system("shutdown /s /t 10") shutdown = True #print('shutdown') else: pyautogui.moveTo(1463 + rod_search[0] + 20, 328 + rod_search[1] + 20, .4) time.sleep(.5) pyautogui.mouseDown(button='right') time.sleep(.1) pyautogui.mouseUp(button='right') time.sleep(.4) pressI() time.sleep(1.5) return
def find_object_in_overview(self, image, coords): object_screenshot = pyautogui.screenshot(region=coords) ingame_object = pyautogui.locate(image, object_screenshot) coordinates = [ coords[0] + ingame_object[0] + (ingame_object[2] / 2), coords[1] + ingame_object[1] + (ingame_object[3] / 2) ] pyautogui.dragTo(coordinates[0], coordinates[1])
def find_image(image, rect=None, loc=None): if rect != None: screenshot = pyautogui.screenshot(region=(rect.left, rect.top, rect.width(), rect.height())) else: screenshot = pyautogui.screenshot(region=loc) location = pyautogui.locate(image, screenshot) return location
def locateonRegion(image, region=None): """在区域region内查找图image""" try: screenshotIm = pg.screenshot(region=region) retVal = pg.locate(image, screenshotIm) return retVal except Exception: return None
def locate(needle: Image, hay: Image, grayscale=True, confidence=0.69): print(f"Needle size: {needle}") print(f"Hay size: {hay}") return pyautogui.locate(needle, hay, grayscale=grayscale, confidence=confidence)
def clickButton(backupButtonDetectionData): topButton = pyautogui.locate('bubble.png', constants.BUTTON_DETECTION_SCREENSHOT_NAME) centered = pyautogui.center(topButton) y = centered.y + backupButtonDetectionData.offsetY x = centered.x + backupButtonDetectionData.offsetX pyautogui.moveTo(x, y) pyautogui.click()
def checkLocation(loc, target): if loc.isEmpty(): loc.setLoc(pyautogui.locateOnScreen(target)) return loc.getLoc() else: return pyautogui.locate( target, pyautogui.screenshot().crop(convert(loc.getLoc())))
def locateAuto(self, sp_img, screenshots, width, height): img = Image.open(BytesIO(sp_img)) img = img.resize( (int(width / 1440 * img.size[0]), int(width / 1440 * img.size[1])), Image.ANTIALIAS) if (pag.locate(img, screenshots, confidence=0.8) != None): return False return True
def read_tile_value(screen, tile): x, y, w, h = tile['location'] # crop the area of the screen containing the tile of interest im = screen.crop((x, y, x + w, y + h)) # compare the cropped tile to all the possible values for value in TILES: if pyautogui.locate(TILES[value], im): tile['value'] = value break
def locate_image(part_image): screen = screenshot() loc = pyautogui.locate(part_image, screen, confidence=0.7, grayscale=True, step=1) if loc is None: return -1, -1 else: loc = pyautogui.center(loc) return loc[0], loc[1]
def find_using_PAG(): with mss.mss() as sct: filename = sct.shot() print(filename) # result = pyautogui.locateCenterOnScreen('C:\GithubProject\PythonPractice\AutoKey\currency_pics\c.png', region=(0,0, 500, 500)) result = pyautogui.locate( 'C:\GithubProject\PythonPractice\AutoKey\currency_pics\c.png', filename) print(result)
def verifyimageonscreen(imagename, confidence=0.85, grayscale=False): haystackImage = pyautogui.screenshot(dir + 'screencapture03.png', region=(405, 135, 1252, 740)) picturelocation = pyautogui.locate(imagename, haystackImage, grayscale=grayscale, confidence=confidence) if picturelocation == None: logging.info("Couldn't locate on screen: " + imagename) return False else: logging.info('found ' +imagename) return True
def findMinimapPosition(): imMinimap = pyautogui.screenshot(region=(minimapLeft + minimapXMargin, minimapTop + minimapYMargin, minimapPreviewWidth, minimapPreviewHeight)) minimapDot = pyautogui.locate('minimapbluedot.png', imMinimap, grayscale=False) return minimapDot
def mine_stuff(self): for img in rock_images: surroundings_img = pag.screenshot('surroundings.png', region=(0,0,1920,1200)) # adjust region so that it covers only the rs env if pag.locate(img, surroundings_img, confidence = 0.85) != None: # if we can find the rock with ore in it self.clickonrock(img) while True: if self.check_ore_exists(img) == False: # if the ore has been mined either by me or someone else break else: continue
def is_mini_program(item): try: time.sleep(0.2) location = pyautogui.locate(needleImage=MINI_PROGRAM_TAG_IMAGE, haystackImage=item) time.sleep(0.2) return location is not None except Exception as e: pass return False
def find_img_in_window(screenshot, img): img_location = pyautogui.locate(img, screenshot) print(img_location) if img_location is None: return bb.BoundingBox([-1, -1, 0, 0], mode='relative') # need to just get the overlay, not the actual game clock # that way it wont matter what the time is return bb.BoundingBox( [img_location[0], img_location[1], img_location[2], img_location[3]], mode='relative')
def touchlist(imglist, pausetime, stage): if stage == 'battling': screenshot() while True: for img_name in imglist: if auto.locate(f'./picture/{Is}/{img_name}','screenshot.png', confidence=c) == None: print('战斗进行中...') screenshot() time.sleep(pausetime) else: print('战斗结束,退出战斗...') return None else: while True: screenshot() for img_name in imglist: p = auto.locate(f'./picture/{Is}/{img_name}', 'screenshot.png', confidence=c) if stage == 'start' and p != None: # 开始阶段 if img_name == 'start_battle.png': # 点击开始战斗按钮 x, y = auto.center(p) tap(x, y) time.sleep(pausetime) break elif img_name == 'os.png': # 点击op按钮 x, y = auto.center(p) tap(x, y) time.sleep(pausetime) return None elif img_name == 'lzbz.png': # 退出理智不足的画面 print('\n----理智不足----') sys.exit(0) elif stage == 'end' and p != None : # 结束阶段 if img_name == 'confidence.png': # 点击结算画面 x, y = auto.center(p) tap(x, y) time.sleep(pausetime) return None elif img_name == 'defeat.png' or img_name == 'jm.png':# 任务失败 和 剿灭作战 tap(hx, hy) time.sleep(pausetime) tap(hx, hy) time.sleep(pausetime) return None
def get_card(debug=True): """获取新抓牌信息 i: p: 截图-逐张寻找-匹配则输出 o: 手牌列表 """ card_image = pyautogui.screenshot(region=(459, 450, 23, 44)) # 国标ver1.30位于左上角, 这是准确值, 无任何余量 for card in mahjong.CARD_LIST: card_position = pyautogui.locate('images\{}.png'.format(card), card_image, grayscale=False) if card_position: if debug: print(card, card_position) #for debug return card
def get_hand(pos, name=None): hand = None img = pyautogui.screenshot(region=(pos[0], pos[1], 40, 40)) for num in reversed(numbers): loc = pyautogui.locate(get_image(num), img, grayscale=True) # if name: # print name + ":", num, loc if loc is not None: fn, fext = os.path.splitext(num) hand = fn break return hand
def find_image(self, screen, image, x=0, y=0, w=None, h=None, center=False): if w is None: right = screen.width else: right = x + w if h is None: bottom = screen.height else: bottom = y + h cropped = screen.crop((x, y, right, bottom)) position = ag.locate(image, cropped) if position != None: if center: return (x + position[0] + position[2] / 2, y + position[1] + position[3] / 2) else: return (x + position[0], y + position[1]) return None
def locate(needle, haystack, locate_all=False, grayscale=False): """ Locates one image in another Args: locate_all (Optional[bool]): Locate All ? needle (Image): The image to find haystack (Image): The image to search in grayscale (Optional[bool]): Use grayscale in matching Returns: Tuple: if found (x, y, width, height) of matching region , otherwise None """ if locate_all: return list(pyautogui.locateAll(needle, haystack, grayscale=grayscale)) else: return pyautogui.locate(needle, haystack, grayscale=grayscale)
def __init__(self): sys.path.append(sys.path[0] + '/Images/') # add image library to path self.height = HS.HEIGHT self.width = HS.WIDTH time.sleep(1) p.click(219, 361) im = p.screenshot('gamestart.png') l,t,w,h = p.locate('exit.png', 'gamestart.png') self.left = l/(1.0+HS.HD_SCREEN) self.top = (t+h-w)/(1.0+HS.HD_SCREEN) + HS.MAC_HEIGHT_BORDER self.right = self.left + self.width self.bottom = self.top + self.height self.minion_height = 126.0/800 * self.height self.minion_width = 104.0/1280 * self.width self.hand_y = self.bottom - 50 self.cards_loc = { 1: {1: 14.2}, 2: {1: 13, 2: 15.4}, 3: {1: 11.9, 2: 14.2, 3: 16.5}, 4: {1: 11, 2: 12.6, 3: 15.8, 4: 17.4}, 5: {1: 10.6, 2: 12.4, 3: 14.2, 4: 16, 5: 17.8}, 6: {1: 10.1, 2: 11.6, 3: 13.1, 4: 14.6, 5: 16.1, 6: 17.6}, 7: {1: 10, 2: 11.3, 3: 12.6, 4: 13.9, 5: 15.2, 6: 16.5, 7: 17.8}, 8: {1: 9.3, 2: 10.5, 3: 11.7, 4: 12.9, 5: 14.1, 6: 15.3, 7: 16.5, 8: 17.7}, 9: {1: 9.3, 2: 10.4, 3: 11.5, 4: 12.6, 5: 13.7, 6: 14.8, 7: 15.9, 8: 17, 9: 18.1}, 10: {1: 9, 2: 10, 3: 11, 4: 12, 5: 13, 6: 14, 7: 15, 8: 16, 9: 17, 10:18} } #cards_file = open('cards.collectible.json','r',encoding='utf-8').read() #self.card_list = json.loads(cards_file) self.threshold_file = open('threshold.txt','r') s = self.threshold_file.read().strip() exec('self.threshold_dict = ' + s)
def __init__(self, blinds = .05): '''Locate the Bovada window and initialize variables ''' ## initialize the bovada logo reference image ## Everytime the bovada frame moves, we must relocate the Bovada logo as a reference point self.BOVADA_REF_IMG = PIL.Image.open("reference_images/BOVADA.png") self.blinds = blinds ## These are the coordinates of where the important values ## exist in the frame, relative to the bovada logo ## top left and bottom right coords of bounding box self.LOCATIONS = { 'hole1' : ((353, 428),(401, 496)), 'hole2' : ((403, 428),(451, 496)), 'board1' : ((269,242), (317, 310)), 'board2' : ((323,242), (371, 310)), 'board3' : ((377,242), (425, 310)), 'board4' : ((431,242), (479, 310)), 'board5' : ((485,242), (533, 310)), 'pot' : ((350, 312), (470, 330)), 'blinds' : ((0, 35), (150, 50)), 'textBar': ((670, 590),(750, 605)), 'fold' : ((420, 560), (520, 580)), 'call' : ((530, 560), (630, 580)), 'check' : ((530, 560), (630, 580)), 'stack0' : ((380, 470), (480, 495)), 'stack1' : ((70, 395), (170, 420)), 'stack2' : ((70, 180), (170, 205)), 'stack3' : ((330, 105), (430, 130)), 'stack4' : ((630, 180), (730, 205)), 'stack5' : ((630, 395), (730, 420)), 'bet0' : ((390, 410),(490, 425)), 'bet1' : ((190, 355), (300, 370)), 'bet2' : ((190, 230), (300, 245)), 'bet3' : ((400, 172), (500, 185)), 'bet4' : ((560, 232), (680, 245)), 'bet5' : ((580, 347), (680, 360)) } ## Coordinates of where dealer button can exist self.DEALER_LOC = [(264, 422),(137,302),(220, 165),(530, 150),(660, 275),(570, 408)] ## save a screen shot os.system("screencapture screenshots/test.jpg") ## locate the bovada symbol on screen to find the bovada window try: self.x,self.y,width,height = gui.locate(self.BOVADA_REF_IMG, PIL.Image.open("screenshots/test.jpg")) except TypeError: raise Exception("Can't locate frame") ## save the bovada application window (as an image) img = misc.imread('screenshots/test.jpg', flatten = True) self.frame = img[self.y:self.y + 650, self.x:self.x + 750] ## load the suit reference functions used to identify card suits self.SUIT_REF_FUNCTIONS = [] heart = misc.imread("reference_images/suits/HEART.jpg", flatten = True) diamond = misc.imread("reference_images/suits/DIAMOND.jpg", flatten = True) spade = misc.imread("reference_images/suits/SPADE.jpg", flatten = True) club = misc.imread("reference_images/suits/CLUB.jpg", flatten = True) self.SUIT_REF_FUNCTIONS.append(('h', heart.sum(axis = 1))) self.SUIT_REF_FUNCTIONS.append(('c', club.sum(axis = 1))) self.SUIT_REF_FUNCTIONS.append(('d', diamond.sum(axis = 1))) self.SUIT_REF_FUNCTIONS.append(('s', spade.sum(axis = 1)))