import numpy as np import os import cv2 as cv from PIL import Image import pydirectinput import random import random from time import time import cv2 as cv import pydirectinput import keyboard def mse(imageA, imageB): # the 'Mean Squared Error' between the two images is the # sum of the squared difference between the two images; # NOTE: the two images must have the same dimension err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) err /= float(imageA.shape[0] * imageA.shape[1]) # return the MSE, the lower the error, the more "similar" # the two images are return err def mse_with_reshape(imageA, imageB): # the 'Mean Squared Error' between the two images is the # sum of the squared difference between the two images; # NOTE: the two images must have the same dimension if len(imageA) == 0 or len(imageB) == 0: return 999 try: imga = np.reshape(imageA, (len(imageA) * 17, 740, 3), 'C') imgb = np.reshape(imageB, (len(imageB) * 17, 740, 3), 'C') err = np.sum((imga.astype("float") - imgb.astype("float")) ** 2) err /= float(imga.shape[0] * imga.shape[1]) # return the MSE, the lower the error, the more "similar" # the two images are return err except: return 999 def load_bl_line(): list_store = [] for path in os.listdir('bl-lines'): if path.endswith(".npy"): list_store.append(np.load("bl-lines\\" + path)) return list_store def check_for_bl_line(line_item, black_list): for bl_item in black_list: if mse(line_item, bl_item) < 500: return True return False def scale_screenshot(screenshot, percent = 200, thresh_mode = True): scale_percent = percent # percent of original size width = int(screenshot.shape[1] * scale_percent / 100) height = int(screenshot.shape[0] * scale_percent / 100) dim = (width, height) resized_img = cv.resize(screenshot, dim, interpolation=4) if thresh_mode: gray = cv.cvtColor(resized_img, cv.COLOR_BGR2GRAY) thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] return thresh # cv.imshow("Tresh", thresh) # cv.waitKey(1000) return resized_img def save_line_item_npy_jpg(short_pic_list): ifr = 1 for x in short_pic_list: np.save("test{}".format(ifr), x) Image.fromarray(x).save("test{}.jpg".format(ifr)) ifr = ifr + 1 def shorten_picture_input(new_pic_lst, old_pic_lst): len_old = len(old_pic_lst) len_new = len(new_pic_lst) offset = 0 offset_diff = len_new - len_old retval = [] while mse_with_reshape(old_pic_lst[offset: len_old], new_pic_lst[0: len_new - offset - offset_diff]) > 500.0: offset = offset + 1 # print(offset) if offset == len(old_pic_lst): break if offset > 0: retval = new_pic_lst[len_new - offset: len_new] return retval def shorten_input(new_lst, old_lst): len_old = len(old_lst) len_new = len(new_lst) offset = 0 offset_diff = len_new - len_old retval = [] while old_lst[offset: len_old] != new_lst[0: len_new - offset - offset_diff]: offset = offset + 1 # print(offset) if offset > 0: retval = new_lst[len_new - offset: len_new] return retval def format_output(data): new_list = data.split("\n") try: while True: # new_list.remove() ("",) new_list.remove('\x0c') except ValueError: pass return new_list def update_screenshot_with_short_pic_list(short_pic_lst, lst_bl_items): screenshot = [] # check if shorten line pictures list has any entries if len(short_pic_lst) > 0: index_lst = [] # check if any of the line item pictures is on the blacklist and save index for x in range(len(short_pic_lst)): if check_for_bl_line(short_pic_lst[x], lst_bl_items): index_lst.append(x) # remove blacklisted items from shorten list r_short_pic_lst = np.delete(short_pic_lst, index_lst, axis=0) # check if further shortened line pictures list has any entries if len(r_short_pic_lst) == 0: return screenshot # combine shorten line picture list to one picture screenshot = np.reshape(r_short_pic_lst, (len(r_short_pic_lst) * 17, 740, 3), 'C') # saves all line item pictures to disk for blacklist evaluation # save_line_item_npy_jpg(r_short_pic_lst) return screenshot def dig_point(point1, point2, dig_time): pydirectinput.moveTo(point1, point2) cv.waitKey(100) pydirectinput.mouseDown() w = random.randint(1, 50) cv.waitKey(dig_time + w) pydirectinput.mouseUp() def check_for_ok_button(cap_win, vis, conf): screenshot = cap_win.get_screenshot() rectangles = vis.find(screenshot, cv.imread("dig/ok_button.jpg", cv.IMREAD_UNCHANGED), 0.5, 1) offset_left = conf.returnOKWindowPos()[2] offset_down = conf.returnOKWindowPos()[3] if len(rectangles) == 1: pointis = vis.get_click_points(rectangles) for pointi in pointis: dig_point(pointi[0] + offset_left, pointi[1] + offset_down, 150) def check_for_craft_button(cap_win, vis): screenshot = cap_win.get_screenshot() needle = cv.imread("craft_table/play.jpg", cv.IMREAD_UNCHANGED) rectangles = vis.find(screenshot, needle, 0.7, 1) if len(rectangles) == 1: pointis = vis.get_click_points(rectangles) for pointi in pointis: dig_point(pointi[0], pointi[1], 150) def check_for_craft_ok_button(cap_win, vis): screenshot = cap_win.get_screenshot() rectangles = vis.find(screenshot, cv.imread("craft_table/ok_button.jpg", cv.IMREAD_UNCHANGED), 0.5, 1) offset_left = 0 offset_down = 0 if len(rectangles) == 1: pointis = vis.get_click_points(rectangles) for pointi in pointis: dig_point(pointi[0] + offset_left, pointi[1] + offset_down, 150) def get_click_point(rectangle): # Loop over all the rectangles x, y, w, h = rectangle # Determine the center position center_x = x + int(w / 2) center_y = y + int(h / 2) # Save the points return int(center_x), int(center_y)