Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (37 sloc)
1.34 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Xlib import display, X | |
from Xlib.ext.xtest import fake_input | |
import cv2 | |
import numpy | |
def find_best(needle, haystack): | |
match = cv2.matchTemplate(haystack, needle, cv2.TM_CCOEFF_NORMED) | |
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(match) | |
return max_loc, max_val | |
def screencap(x, y, w, h): | |
root = display.Display().screen().root | |
raw = root.get_image(x, y, w, h, X.ZPixmap, 0xffffffff) | |
return numpy.fromstring(raw.data, dtype=numpy.uint8).reshape(h, w, 4)[:, :, :3] | |
def mouse_move(x, y): | |
disp = display.Display() | |
root = disp.screen().root | |
root.warp_pointer(x, y) | |
disp.sync() | |
def mouse_click(button=1): | |
disp = display.Display() | |
fake_input(disp, X.ButtonPress, button) | |
disp.sync() | |
fake_input(disp, X.ButtonRelease, button) | |
disp.sync() | |
def find_on_screen(image): | |
needle = cv2.imread(image) | |
screen = display.Display().screen() | |
width, height = screen.width_in_pixels, screen.height_in_pixels | |
haystack = numpy.array(screencap(0, 0, width, height)) | |
return needle.shape[1::-1], find_best(needle, haystack) | |
def click(image, similarity=0.7): | |
shape, (pos, confidence) = find_on_screen(image) | |
if confidence > similarity: | |
center = [int(p + s/2) for p, s in zip(pos, shape)] | |
mouse_move(*center) | |
mouse_click() | |
if __name__ == '__main__': | |
click("image.png") |