def teach_me_how_to_read(im): im = im.convert('RGB') start = time.time() all_letters = list(string.ascii_uppercase) results = [] # TODO add this for the real thing for performance # , region=(300, 0, 500, 300) # , region=(300, 0, 500, 300) for key in all_letters: for hit in pyautogui.locateAll("keys/" + key + "_lower_key.png", im, confidence=0.96, grayscale=True): results.append({ 'capital': False, 'x': hit.left, 'y': hit.top, 'letter': key.lower(), 'width': hit.width, 'color': get_color(im, hit.left, hit.top, hit.width, hit.height) }) for hit in pyautogui.locateAll("keys/" + key + "_upper_key.png", im, confidence=0.96, grayscale=True): results.append({ 'capital': True, 'x': hit.left, 'y': hit.top, 'letter': key, 'width': hit.width, 'color': get_color(im, hit.left, hit.top, hit.width, hit.height) }) sorted_results = bucket(results) fully_baked_output = build_results(sorted_results) # TODO: Make togglable? # print_results(fully_baked_output) return fully_baked_output
def identifyNumbers(imgLife, imgMana, vector_life, vector_mana): for x in range(0, 10): vector_life[x] = pyautogui.locateAll(path + '/images/' + str(x) + '.png', imgLife, grayscale=True, confidence=.95) vector_mana[x] = pyautogui.locateAll(path + '/images/' + str(x) + '.png', imgMana, grayscale=True, confidence=.95)
def checkBotRight(checks_temp): global checks, debug_mode, width, height #if debug_mode: print(str(time.time()) + ": Start Bot-Right") region = pyautogui.screenshot(region=(width / 2, 0, width / 2, height)) if browser.lower() == "chrome": c = list(pyautogui.locateAll("chrome/check1.png", region)) c.extend(list(pyautogui.locateAll("chrome/check2.png", region))) elif browser.lower() == "edge": c = list(pyautogui.locateAll("edge/check.png", region)) for i in c: i = modBox(i.left + width / 2, i.top, i.width, i.height) checks_temp.append(i)
def pick(): # Png samples that will be located on screenshoot highlight_path = './img/highlight/*.png' img_highlight_needles = glob.glob(highlight_path, recursive=False) with mss.mss() as sct: # The screen part to capture region = { "top": poe.top, "left": poe.left, "width": poe.width, "height": poe.height } # Grab the data sct_img = sct.grab(region) # Create the Image in PIL format screenshot = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX") # Save mouse position mouse_init_pos = pyautogui.position() for img_highlight in img_highlight_needles: items = list(pyautogui.locateAll(img_highlight, screenshot)) click_items(items) pyautogui.moveTo(mouse_init_pos)
def find_every_bitmap(haystack: Image, needle: Image, confidence: float) -> list[(int, int, int, int)]: return list( pyautogui.locateAll(needle, haystack, grayscale=False, confidence=confidence))
def findBoardLetters(boardim, confidence): print("Finding Board Letters...") letterPos = [] checkletters = "abdefgiklmnoprstvyxz" lf = 0 for c in checkletters: print("Finding all " + c) letterim = Image.open("letters/{}.png".format(c)) fn = lambda x: 255 if x > 200 else 0 letterim = letterim.convert("L").point(fn, mode="1") boardim = boardim.convert("L").point(fn, mode='1') loc = list( pyautogui.locateAll(letterim, boardim, True, confidence=confidence)) loc = filterLocations(loc) print(loc) for p in loc: letterPos.append((p, c)) lf += len(loc) print("Found " + str(lf) + " characters!") if (lf != 16): raise Exception("Expected 16 characters, found {}".format(str(lf))) return sorted(letterPos)
def updateValue(self, table_img): self.value_container.numbers = [] if not (self.is_available): return numbers_list = [] table_img_portion = table_img.crop( (self.box.left, self.box.top, self.box.left + self.box.width, self.box.top + self.box.height)) if (True): #Attempt to locate stack for i, file in enumerate(range(10)): file = str(i) + '.png' for j, box in enumerate( pyautogui.locateAll( constants.NUMBERS_BUTTON_PATH + file, table_img_portion, confidence=constants.NUMBERS_BUTTON_DET_CONFIDENCE) ): if (box != None): numbers_list.append(Number(value=i, box=box)) else: pass else: pass numbers_list.sort(key=lambda number: number.left) #print([number.value for number in numbers_list]) for number in numbers_list: self.value_container.addNumber(number) self.value_container.computeValue() self.value_container.corresponding_entity = self.id self.value = self.value_container.value print('[Button] ' + str(self.id) + ' holds value: ' + str(self.value)) return
def find(screenshot): # dictionary for digits grouped by "y" found = dict() # find all digits for digit in range(10): positions = pyautogui.locateAll('digits/{}.png'.format(digit), screenshot, True) for x, y, _, _ in positions: if y not in found: found[y] = dict() found[y][x] = digit # recreate values result = [] for row in sorted(found): cols = sorted(found[row]) value = ''.join(str(found[row][col]) for col in cols) result.append(value) return result
def Main(im, Files, Sub, Dom, Cords, ListOfCycles, c_killfeed, KFPath, Debug=False): try: newtime = time() if Debug == True else 0 for file in Files: newtime = LogTime(file, Files, newtime) if Debug == True else 0 for userloc in locateAll(file, im, confidence=0.9, region=Cords): x, y = center(userloc) if not 'Assist' in file: TeamColor = GenTeamColor(im, x - 35, y) red, green, blue = im.getpixel((int(x) - 70, int(y))) if red > 250 and green > 245 and blue > 240 or \ red > 250 and green < 20 and 30 < blue < 50: name = file.replace(KFPath, '').replace('.png', '') if TeamColor == 'Blue': TrackDeath(name, TeamColor, Sub, Dom, ListOfCycles, c_killfeed) else: TeamColor = GenTeamColor(im, x - 15, y) name = file.replace(KFPath, '').replace('.png', '') print(Sub, name) if Sub in name and TeamColor == 'Blue': TreatSub('Assist', name, ListOfCycles, c_killfeed) except KeyboardInterrupt: pass
def searchAllPlayers(table_img): glob_file.all_players_found = False glob_file.players = [] player_boxes = [] known_positions = [] #players_found=0 for file in os.listdir(constants.PLAYER_IMAGE_PATH): for j, box in enumerate( pyautogui.locateAll( constants.PLAYER_IMAGE_PATH + file, table_img, confidence=constants.PLAYER_DET_CONFIDENCE)): #print(box) known = False for known_position in known_positions: #avoid spotting twice the same if ((box.left > (known_position["left"] - 10) and box.left < (known_position["left"] + 10)) and (box.top > (known_position["top"] - 10) and box.top < (known_position["top"] + 10))): # known = True break if (not (known)): player_boxes.append(box) known_positions.append({'left': box.left, 'top': box.top}) if (len(player_boxes) == constants.NB_PLAYERS): glob_file.all_players_found = True print("# All players found and classes created #") if (len(player_boxes) <= constants.NB_PLAYERS): print(str(len(player_boxes)) + " players found") #for i in range(constants.NB_PLAYERS-len(player_boxes)): # player_boxes.append(Box(0,0,0,0)) #elif len(player_boxes)==constants.NB_PLAYERS: player_boxes.sort( key=lambda box: computeBoxAngle(box, glob_file.table.center_pos), reverse=True) #player_boxes.sort(key=lambda box: box.top, reverse=True) # player_2, player_3,player_5 = player_boxes[3], player_boxes[5], player_boxes[2] # player_boxes[2], player_boxes[3], player_boxes[5] = player_2, player_3, player_5 for i, box in enumerate(player_boxes): #computeBoxAngle(box, glob_file.table.center_pos) glob_file.players.append( Player( id_=i, image_file_path=constants.HOLE_CARDS_IMAGE, detection_confidence=constants.HOLE_CARDS_DET_CONFIDENCE, player_box=box, table_img=table_img)) else: print("[Warning] Found too many players") pass return
def find(screenshot): # dictionary for digits grouped by "y" found = dict() # find all digits for digit in range(10): positions = pyautogui.locateAll('digits/{}.png'.format(digit), screenshot, True) for x,y,_,_ in positions: if y not in found: found[y] = dict() found[y][x] = digit # recreate values result = [] for row in sorted(found): cols = sorted(found[row]) value = ''.join(str(found[row][col]) for col in cols) result.append(value) return result
def read_num_jumps(): jumps_image = pyautogui.screenshot(region=DEFAULT_DOCKED_ROUTE_BLOCKS_REGION) # jumps_image = Image.open('uielements/route_blocks.png') jumps_image = level_image(jumps_image, 140, 255, 1) # ImageEnhance.Contrast. jumps_image = ImageEnhance.Color(jumps_image).enhance(0) jumps_image = ImageEnhance.Brightness(jumps_image).enhance(1) jumps_image = ImageEnhance.Contrast(jumps_image).enhance(4) # ImageEnhance. jumps_image.save('test_img.png') jump_squares = pyautogui.locateAll(needleImage='uielements/route_block.png', haystackImage=jumps_image, grayscale=True, confidence=0.70) jump_squares = list(jump_squares) print(f'Found {len(list(jump_squares))} jumps') # return print('Iterating over squares') for square in jump_squares: center = get_box_center(square) pyautogui.moveTo(center[0] + DEFAULT_DOCKED_ROUTE_BLOCKS_REGION[0], center[1] + DEFAULT_DOCKED_ROUTE_BLOCKS_REGION[1], 0) time.sleep(0.25) pyautogui.moveTo(100,100) # time.sleep(3) # print(dir(jump_squares)) pass
def get_screen(debug=False, max_block=2048): block_matrix = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] im = pyautogui.screenshot(region=(200, 250, 1020, 1220)) # screenshot im.save("screen.png") title = pyautogui.locateOnScreen("images/title.png", im, grayscale=True) print(title) pyautogui.moveTo([title[0] / 2, title[1] / 2]) print(type(im)) return for image in os.listdir("images"): # match for blocks num = re.match("\d+", image) if not num: continue else: num = int(num.group()) if num > max_block * 2: continue if debug: print(image) block = pyautogui.locateAll("images/"+image, im, grayscale=True) for pos in block: if debug: print(pos) index = pos_to_shape(pos, debug=False) block_matrix[index[1]][index[0]] = num if debug: print(block_matrix) return block_matrix
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 recognize(screen_shot_im): n_im = 1 for targ_im in glob.glob(".\\targartimage\\*.png"): im = Image.open(targ_im) im_b = Image.open(screen_shot_im) # im_bx, im_by = im_b.size llist = list( pyautogui.locateAll(im, im_b, grayscale=True, region=(168, 160, 181, 497))) # 1366 x 768 分辨率下 所有匹配图像的坐标生成列表 if llist: for eachlist in llist: imr = im_b.crop(( eachlist[0], eachlist[1], eachlist[0] + imWidth, eachlist[1] + imHeight, )) # 截取形成的信息图像,宽度为778,高度为22 imcode = im_b.crop(PatientIdRegion) # 截取住院号,宽度为74 imr.paste(imcode, (imWidth - patientIdwidth, 1)) # 粘贴住院号,粘贴到信息条的最右端,横坐标为两者宽度相减:778-74 imr.save(".\\results\\done\\" + re.split(r"\\|/", screen_shot_im)[-3] + "_" + re.split(r"\\|/", screen_shot_im)[-1][:-4] + "_" + str(n_im) + ".png") n_im += 1
def get_sq_size(): input('Move your mouse to the top left square and press Enter.') currentMouseX, currentMouseY = pyautogui.position() print('Thank you. You can use your mouse now.') img = pyautogui.screenshot() color = img.getpixel((currentMouseX, currentMouseY)) left = currentMouseX right = currentMouseX top = currentMouseY bot = currentMouseY while img.getpixel((left, top)) == color: left -= 1 left += 1 while img.getpixel((right, top)) == color: right += 1 right -= 1 while img.getpixel((left, top)) == color: top -= 1 top += 1 # while img.getpixel((left, bot)) == color: # bot += 1 # bot -= 1 n = len(list(pyautogui.locateAll('imgs/cross.jpg', img))) n //= 16 n **= 0.5 n = round(n) n *= 5 return right - left + 1, (left, top), n
def confirm_help_grid(): getPhoneScreen("screenshot.png") transPhoneScreen(andriodFileName="screenshot.png", transFileName="chat.png", transPicPath=BASE_FILE_PATH) coords = pyautogui.locateAll(RESOURCES_PATH + "/help/help2.png", BASE_FILE_PATH + "chat.png", grayscale=False) logging.info(f"(聊天)窗口,增援请求个数 count-{len(list(coords))}") flag = False for i in list(coords): x_location, y_location = pyautogui.center(i) slideOnPhone(x_location, y_location, x_location, y_location) # 默认点击三行三列 pointOnPhone(876, 196) pointOnPhone(876, 375) pointOnPhone(876, 595) pointOnPhone(1015, 196) pointOnPhone(1015, 375) pointOnPhone(1015, 595) pointOnPhone(1150, 196) pointOnPhone(1150, 375) pointOnPhone(1150, 595) slideOnPhone(x_location, y_location, x_location, y_location) flag = True return flag
def pngtoint(): hjhj = "money.png" try: zero = list(pyautogui.locateAll("zero.png", hjhj, confidence = 0.86)) one = list(pyautogui.locateAll("one.png", "money.png", confidence = 0.85)) two = list(pyautogui.locateAll("two.png", "money.png", confidence = 0.9)) three = list(pyautogui.locateAll("three.png", "money.png", confidence = 0.82)) four = list(pyautogui.locateAll("four.png", "money.png", confidence = 0.86)) five = list(pyautogui.locateAll("five.png", "money.png", confidence = 0.88)) six = list(pyautogui.locateAll("six.png", "money.png", confidence = 0.9)) seven = list(pyautogui.locateAll("seven.png", "money.png", confidence = 0.86)) eight = list(pyautogui.locateAll("eight.png", "money.png", confidence = 0.85)) nine = list(pyautogui.locateAll("nine.png", "money.png", confidence = 0.88)) number= [] for x in zero: number.append((x[0],"0")) for x in one: number.append((x[0],"1")) for x in two: number.append((x[0],"2")) for x in three: number.append((x[0],"3")) for x in four: number.append((x[0],"4")) for x in five: number.append((x[0],"5")) for x in six: number.append((x[0],"6")) for x in seven: number.append((x[0],"7")) for x in eight: number.append((x[0],"8")) for x in nine: number.append((x[0],"9")) number.sort() final = [] for x in number: final.append(x[1]) final = "".join(final) final = int(final) return(final) except: return(-1)
def find_all_items(screen, item): all_list = [] confidence = item.get_confidence() for i in item.imglist: try: for pos in pyautogui.locateAll(i, screen, confidence=confidence): all_list.append(pos) except pyautogui.ImageNotFoundException: pass return all_list
def searchAllOpenCards(table_img): glob_file.all_cards_found = False glob_file.cards = [] card_colors = ['heart', 'diamond', 'club', 'spade'] known_positions = [] for i, card_color in enumerate(card_colors): if (True): for j, box in enumerate( pyautogui.locateAll( constants.CARD_PATH + card_color + '.png', table_img, confidence=constants.CARD_DET_CONFIDENCE)): known = False for known_position in known_positions: #avoid spotting twice the same if ((box.left > (known_position["left"] - box.width / 2) and box.left < (known_position["left"] + box.width / 2)) and (box.top > (known_position["top"] - box.height / 4) and box.top < (known_position["top"] + box.height / 4))): # known = True break if (not (known)): known_positions.append({'left': box.left, 'top': box.top}) new_card = Card(id_='card_' + card_color + '_' + str(j), color=card_color[0], box=box, table_img=table_img) glob_file.cards.append(new_card) else: pass glob_file.cards.sort(key=lambda x: x.box.left) glob_file.cards.sort(key=lambda x: x.isHeroCard, reverse=True) #print("There are : "+str(len(cards))+ " cards") if (len(glob_file.cards) >= 2): if (glob_file.cards[0].box.left > glob_file.cards[1].box.left): card_0, card_1 = glob_file.cards[1], glob_file.cards[0] glob_file.cards[1], glob_file.cards[0] = card_1, card_0 if (len(glob_file.cards) == 7): #All cards were found, creating classes for speedup glob_file.all_cards_found = True for i, card in enumerate(glob_file.cards): card.setId(i) print("# All cards found and classes created #") if (len(glob_file.cards) > 7): print("[Warning] Too many cards spotted!") return
def findOtherCards(pos): # 检测pos内的牌 global cards, lamp print(pos) otherCardsNum = {'rdw': 0, 'bxw': 0, 'b2': 0, 'r2': 0, 'bA': 0, 'rA': 0, 'bK': 0, 'rK': 0, 'bQ': 0, 'rQ': 0, 'bJ': 0, 'rJ': 0, 'b10': 0, 'r10': 0, 'b9': 0, 'r9': 0, 'b8': 0, 'r8': 0, 'b7': 0, 'r7': 0, 'b6': 0, 'r6': 0, 'b5': 0, 'r5': 0, 'b4': 0, 'r4': 0, 'b3': 0, 'r3': 0, } lamp.config(background='red') time.sleep(waitTime) img = pyautogui.screenshot(region=pos) for i in otherCardsNum.keys(): result = pyautogui.locateAll(needleImage='pics\\o' + i + '.png', haystackImage=img, confidence=otherConfidence) otherCardsNum[i] = cardsFilter(list(result), otherFilter) cards[1] -= otherCardsNum['bA'] + otherCardsNum['rA'] cards[2] -= otherCardsNum['b2'] + otherCardsNum['r2'] cards[3] -= otherCardsNum['b3'] + otherCardsNum['r3'] cards[4] -= otherCardsNum['b4'] + otherCardsNum['r4'] cards[5] -= otherCardsNum['b5'] + otherCardsNum['r5'] cards[6] -= otherCardsNum['b6'] + otherCardsNum['r6'] cards[7] -= otherCardsNum['b7'] + otherCardsNum['r7'] cards[8] -= otherCardsNum['b8'] + otherCardsNum['r8'] cards[9] -= otherCardsNum['b9'] + otherCardsNum['r9'] cards[10] -= otherCardsNum['b10'] + otherCardsNum['r10'] cards[11] -= otherCardsNum['bJ'] + otherCardsNum['rJ'] cards[12] -= otherCardsNum['bQ'] + otherCardsNum['rQ'] cards[13] -= otherCardsNum['bK'] + otherCardsNum['rK'] cards[14] -= otherCardsNum['bxw'] cards[15] -= otherCardsNum['rdw']
def find_item(self, item): title = self._get_title_dimension() title_x, title_y = pyautogui.center(title) mouse.move((randint( title_x - 30, title_x + 30), randint(title_y - 4, title_y + 4)), 2) pyautogui.click() return [(randint(title.left + x.left, title.left + x.left + 15), randint(title.top + x.top + 3, title.top + x.top + 7)) for x in pyautogui.locateAll( item, self._get_window_image(), confidence=.9)]
def findMyCards(): global cards, lamp myCardsNum = {'rdw': 0, 'bxw': 0, 'b2': 0, 'r2': 0, 'bA': 0, 'rA': 0, 'bK': 0, 'rK': 0, 'bQ': 0, 'rQ': 0, 'bJ': 0, 'rJ': 0, 'b10': 0, 'r10': 0, 'b9': 0, 'r9': 0, 'b8': 0, 'r8': 0, 'b7': 0, 'r7': 0, 'b6': 0, 'r6': 0, 'b5': 0, 'r5': 0, 'b4': 0, 'r4': 0, 'b3': 0, 'r3': 0, } lamp.config(background='red') img = pyautogui.screenshot(region=myPos) for i in myCardsNum.keys(): result = pyautogui.locateAll(needleImage='pics\\m' + i + '.png', haystackImage=img, confidence=myConfidence) myCardsNum[i] = cardsFilter(list(result), myFilter) cards[1] -= myCardsNum['bA'] + myCardsNum['rA'] cards[2] -= myCardsNum['b2'] + myCardsNum['r2'] cards[3] -= myCardsNum['b3'] + myCardsNum['r3'] cards[4] -= myCardsNum['b4'] + myCardsNum['r4'] cards[5] -= myCardsNum['b5'] + myCardsNum['r5'] cards[6] -= myCardsNum['b6'] + myCardsNum['r6'] cards[7] -= myCardsNum['b7'] + myCardsNum['r7'] cards[8] -= myCardsNum['b8'] + myCardsNum['r8'] cards[9] -= myCardsNum['b9'] + myCardsNum['r9'] cards[10] -= myCardsNum['b10'] + myCardsNum['r10'] cards[11] -= myCardsNum['bJ'] + myCardsNum['rJ'] cards[12] -= myCardsNum['bQ'] + myCardsNum['rQ'] cards[13] -= myCardsNum['bK'] + myCardsNum['rK'] cards[14] -= myCardsNum['bxw'] cards[15] -= myCardsNum['rdw']
def item_finder(): global x_schieber, y_schieber, arc_count, run, finder_count finder_count = finder_count + 1 if pyautogui.pixelMatchesColor(int(ench_window_x + 10 + x_schieber * 37), int(ench_window_y + 51 + y_schieber * 35), (16, 16, 16)): # for proc in psutil.process_iter(): # # check whether the process name matches # if proc.name() == 'l2.bin' or proc.name() == 'l2.exe': # print('L2 Prozess beenden') # proc.kill() # with open(path + "bot.run", "w") as fh: # fh.write(str(0)) print('Keine Gegenstände mehr zum verbessern! Beende Spiel') # subprocess.call(["shutdown", "/s"]) input() pyautogui.screenshot('ench_screen.png', region=(ench_window_x + 10 + x_schieber * 37, ench_window_y + 51 + y_schieber * 35, 12, 8)) picture = Image.open("ench_screen.png") width, height = picture.size for x in range(0, width): for y in range(0, height): current_color = picture.getpixel( (x,y) ) if current_color != (255,0,0): picture.putpixel( (x,y), (0, 0, 0)) picture.save("ench_screen.png") temp = pyautogui.locateAll(path + "pic\\" + max_enchant, "ench_screen.png", grayscale=False) i = len(list(temp)) if i == 1: arc_count = arc_count + 1 print(str(y_schieber) + ' Gegenstand gefunden ' + str(x_schieber)) x_schieber = x_schieber + 1 if x_schieber != 0 and x_schieber % 6 == 0: y_schieber = y_schieber + 1 x_schieber = 0 if run < 5 or finder_count == 1: finder_count = 0 run = run - 1 else: requests.get('http://s.leichtbewaff.net/?stat='+str(run), verify=False) run = 0 # Wenn enchant_max erreicht Cancel im Enchant Fenster pyautogui.moveTo(ench_window_x + 170, ench_window_y + 383) mausklick() time.sleep(v + ok) #0.25
def locate_all(self, img_to_locate): self_screenshot = self.take_screenshot() all_located = agui.locateAll(img_to_locate, self_screenshot, confidence=0.6, grayscale=True) located_bboxes = [ BoundingBox( left=left + self.x, top=top + self.y, width=width, height=height ) for left, top, width, height in all_located ] return located_bboxes
def findEndPortals(screenshots): img = screenshots["stronghold"] portals = pyautogui.locateAll(configFile("EndPortal.png"), img) portalCenters = [] for i in portals: x = round(i.left + i.width/2) z = round(i.top + i.height/2) x = round (x/screenDivider) z = round (z/screenDivider) portalCenters.append({"x": x, "z": z}) return portalCenters
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 read_board(reg, gameBoard, game_info, direct): """ Updates internal list from board read Input: reg - location of board, gameBoard - object tuple of board state, game_info - information about current game settings Output: makes changes to array returns notheing """ mines = 0 other = 0 auto.moveTo(1, 1) #move mouse out of the way for screen shot #TODO make so game board in top left dosn't cause errors currentGameState = auto.screenshot(region=reg) for key in cell_types: foundCells = auto.locateAll(direct + "/" + cell_types[key], currentGameState) if (foundCells != None): for tup in foundCells: x = int((tup[0]) / (reg[2] / (game_info.width))) y = int((tup[1]) / (reg[3] / (game_info.height))) gameBoard[x][y].currentValue = key
def locate(chess_png): # global board_screenshot # if board_screenshot is None: # left, top, right, bottom = find_QQChess() # if right - left == 0: # raise Exception('QQChess window not found') # board_screenshot = pyautogui.screenshot( # region=(left, top, right - left, bottom - top)) found = list( pyautogui.locateAll(chess_png[1], board_screenshot, confidence=0.80)) ret = [] for f in found: if color_match(chess_png[0], f): f = pyautogui.center(f) grid = pix2grid(f) if grid != None: ret.append((chess_png[0], grid)) return ret
def findSiwtchPostion(self): if self.__win is not None: window_img = switch_img = None try: window_img = pyautogui.screenshot(region=self.__win.box) switch_img = Image.open("D:\\py_pro\\3.jpg") topleft = self.__win.topleft for e in pyautogui.locateAll(switch_img, window_img, grayscale=True): print(e) p = (topleft.x + e.left + e.width // 2, topleft.y + e.top + e.height // 2) self.click_positions.append(p) finally: if window_img: window_img.close() if switch_img: switch_img.close()
def allpicsmallwindow(haystackImage, name, valor=None): listcoords = [] needleImage = name confidence = 0.99 if valor is None else valor try: picturelocations = pyautogui.locateAll(needleImage, haystackImage, grayscale=False, confidence=confidence) for picturelocation in picturelocations: #logging.info(picturelocation) picturepoint = pyautogui.center(picturelocation) pic_x, pic_y = picturepoint pic_x += 405 pic_y += 135 listcoords.append([pic_x,pic_y]) ##logging.info(str(pic_x) + ',' + str(pic_y)) sleep(0.3) return listcoords except: #logging.info(traceback.print_exc()) logging.info("there was an error with locating image: " + needleImage) return listcoords
def get_hand(debug=False): """获取手牌信息 i: p: 截图-逐张寻找-添加, 时间大约1s, 每局一次, 因此目前可接受. o: 手牌列表 """ hand_image = pyautogui.screenshot(region=(119, 450, 323, 44)) #截取手牌图, 国标ver1.30位于左上角时, 坐标 (118, 449). #牌25, 46, 但只取23, 44 hand = [] for card in mahjong.CARD_LIST: card_position = pyautogui.locateAll('images\{}.png'.format(card), hand_image, grayscale=True) num_card = len(list(card_position)) if num_card: hand.append(card * num_card) if len(hand) == 13: # 如果已经到十三张, 说明已完成, 可提前结束. break if debug: print(''.join(hand)) return ''.join(hand)