Guest User

windowcapture.py

a guest
May 13th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. windowcapture.py
  2.  
  3. import numpy as np
  4. import win32gui
  5. import cv2 as cv
  6. import pyautogui
  7.  
  8.  
  9. class WindowCapture:
  10.  
  11.     # properties
  12.     hwnd = None
  13.     w = 0
  14.     h = 0
  15.     cropped_x = 0
  16.     cropped_y = 0
  17.     offset_x = 0
  18.     offset_y = 0
  19.  
  20.     # constructor
  21.     def __init__(self, window_name='League of Legends (TM) Client'):
  22.  
  23.         # wait for window to appear
  24.         while not self.hwnd:
  25.             self.hwnd = win32gui.FindWindow(None, window_name)
  26.  
  27.         win32gui.SetForegroundWindow(self.hwnd)
  28.  
  29.         # get the window size
  30.         window_rect = win32gui.GetWindowRect(self.hwnd)
  31.         self.w = window_rect[2] - window_rect[0]
  32.         self.h = window_rect[3] - window_rect[1]
  33.  
  34.         # account for the window border and titlebar and cut them off
  35.         border_pixels = 8
  36.         titlebar_pixels = 30
  37.         self.w = self.w - (border_pixels * 2)
  38.         self.h = self.h - titlebar_pixels - border_pixels
  39.         self.cropped_x = border_pixels
  40.         self.cropped_y = titlebar_pixels
  41.  
  42.         # set the cropped coordinates offset so we can translate screenshot
  43.         # images into actual screen positions
  44.         self.offset_x = window_rect[0] + self.cropped_x
  45.         self.offset_y = window_rect[1] + self.cropped_y
  46.        
  47.  
  48.     @property
  49.     def screenshot(self):
  50.         x, y, x1, y1 = win32gui.GetClientRect(self.hwnd)
  51.         x, y = win32gui.ClientToScreen(self.hwnd, (x, y))
  52.         x1, y1 = win32gui.ClientToScreen(self.hwnd, (x1 - x, y1 - y))
  53.         img = pyautogui.screenshot(region=(x, y, x1, y1))
  54.         img = np.array(img)
  55.         img = cv.cvtColor(img, cv.COLOR_RGB2BGR)
  56.         return img
  57.  
  58.     # find the name of the window you're interested in.
  59.     # once you have it, update window_capture()
  60.     # https://stackoverflow.com/questions/55547940/how-to-get-a-list-of-the-name-of-every-open-window
  61.     @staticmethod
  62.     def list_window_names():
  63.         def winEnumHandler(hwnd, ctx):
  64.             if win32gui.IsWindowVisible(hwnd):
  65.                 print(hex(hwnd), win32gui.GetWindowText(hwnd))
  66.         win32gui.EnumWindows(winEnumHandler, None)
  67.  
  68.     # translate a pixel position on a screenshot image to a pixel position on the screen.
  69.     # pos = (x, y)
  70.     # WARNING: if you move the window being captured after execution is started, this will
  71.     # return incorrect coordinates, because the window position is only calculated in
  72.     # the __init__ constructor.
  73.     def get_screen_position(self, pos):
  74.         return (pos[0] + self.offset_x, pos[1] + self.offset_y)
Advertisement
Add Comment
Please, Sign In to add comment