Created
August 29, 2014 05:59
-
-
Save DanBrink91/f4763f6f30b69355a287 to your computer and use it in GitHub Desktop.
Find the location (Y, X) of target image on the screen by taking a screenshot
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
import gtk.gdk | |
import numpy as np | |
import cv2 | |
import subprocess | |
w = gtk.gdk.get_default_root_window() | |
sz = w.get_size() | |
# Get screen pixels | |
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, sz[0], sz[1]) | |
pb = pb.get_from_drawable(w, w.get_colormap(), 0, 0, 0, 0, sz[0], sz[1]) | |
screen_pixels = pb.get_pixels_array() | |
# now read in target | |
target = cv2.imread("sublime2.png") | |
# find match | |
result = cv2.matchTemplate(screen_pixels, target, cv2.TM_CCOEFF_NORMED) | |
coords = np.unravel_index(result.argmax(), result.shape) | |
# move mouse to the match coordinates | |
subprocess.call(['xdotool', 'mousemove', str(coords[1]), str(coords[0])]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment