Window Capture için:
Ardından bu kodu, başka bir Python dosyasına şu kodları kullanarak çalıştırabilirsiniz:
Python:
import numpy as np
import win32gui, win32ui, win32con
class WindowCapture:
w = 0
h = 0
hwnd = None
cropped_x = 0
cropped_y = 0
offset_x = 0
offset_y = 0
def __init__(self, window_name=None):
self.hwnd = win32gui.FindWindow(None, window_name)
if not self.hwnd:
raise Exception('Window not found: {}'.format(window_name))
window_rect = win32gui.GetWindowRect(self.hwnd)
self.w = window_rect[2] - window_rect[0]
self.h = window_rect[3] - window_rect[1]
border_pixels = 3
titlebar_pixels = 10
self.w = self.w - (border_pixels * 2)
self.h = self.h - titlebar_pixels - border_pixels
self.cropped_x = border_pixels
self.cropped_y = titlebar_pixels
self.offset_x = window_rect[0] + self.cropped_x
self.offset_y = window_rect[1] + self.cropped_y
def get_screenshot(self):
wDC = win32gui.GetWindowDC(self.hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (self.cropped_x, self.cropped_y), win32con.SRCCOPY)
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = (self.h, self.w, 4)
# free resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(self.hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
img = img[...,:3]
img = np.ascontiguousarray(img)
return img
def list_window_names(self):
def winEnumHandler(hwnd, ctx):
if win32gui.IsWindowVisible(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
win32gui.EnumWindows(winEnumHandler, None)
def get_screen_position(self, pos):
return (pos[0] + self.offset_x, pos[1] + self.offset_y)
Ardından bu kodu, başka bir Python dosyasına şu kodları kullanarak çalıştırabilirsiniz:
Python:
import cv2
from time import time
from windowcapture import WindowCapture #from python window capture dosyanızın adı ne ise
window_capture = WindowCapture("game_name")#oyun adını yazın game_name kısmına
def get_screen():
loop_time = time()
while True:
screenshot = window_capture.get_screenshot()
frame = cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB)
cv2.imshow("window", frame)
print('FPS {}'.format(1 / (time() - loop_time)))
loop_time = time()
if cv2.waitKey(1) == ord('q'):
cv2.destroyAllWindows()
break
get_screen()
Moderatörün son düzenlenenleri: