''' Created on Jul 23, 2018 @author: havrila ''' # pip install .\pywin32-223.1-cp37-cp37m-win32.whl from https://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32 import win32gui #pip install opencv-python import cv2 #pip install mss from mss import mss sct = mss() #pip install numpy import numpy as np ''' STANDARDS ''' from PIL import Image import time class window_capture_wrapper: @staticmethod def calculate_pos(window_handler): rect = win32gui.GetWindowRect(window_handler) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y xx = rect[2] yy = rect[3] ''' Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0). ''' rect = win32gui.GetClientRect(window_handler) border_size = int((w - rect[2]) / 2) panel_size = h - border_size - rect[3] # now we know the bottom right start of real window x = x + border_size y = y + panel_size w = rect[2] h = rect[3] xx = x + w yy = y + h pos = {'top': y, 'left': x, 'width': w, 'height': h} return pos @staticmethod def find_window(window_name): window_handler = win32gui.FindWindow(None,window_name) return window_handler ''' This is the main execution thread ''' if __name__ == '__main__': ''' Try finding the window needed ''' try: window = window_capture_wrapper.find_window("Elite - Dangerous (CLIENT)") except: print("Failed to find the right window, exiting") quit(1) ''' loop of getting snapshots ''' index = 0 current_milli_time = lambda: int(round(time.time() * 1000)) last_time = current_milli_time() while True: ''' Indexing and time ''' index = index + 1 current_time = current_milli_time() last_time = current_time ''' Image grabbing ''' sct_img = sct.grab(window_capture_wrapper.calculate_pos(window)) img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'RGBX') ''' DRAWING EXAMPLES ''' np_array = np.array(img) np_array = cv2.rectangle(np_array,(384,0),(510,128),(0,255,0),3) ''' PRINT IMAGE ''' cv2.imshow('test', np_array) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.destroyAllWindows() break