Created
April 14, 2017 18:31
-
-
Save franneck94/a8d8f631dc6c3cec38e2fadc5f85c5a4 to your computer and use it in GitHub Desktop.
ImageGrabber for PyGTA
This file contains hidden or 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 win32gui, win32ui, win32con, win32api | |
| import numpy as np | |
| import cv2 | |
| def grab_screen(region=None): | |
| hwin = win32gui.GetDesktopWindow() | |
| if region: | |
| left,top,x2,y2 = region | |
| width = x2 - left + 1 | |
| height = y2 - top + 1 | |
| else: | |
| width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) | |
| height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) | |
| left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN) | |
| top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) | |
| hwindc = win32gui.GetWindowDC(hwin) | |
| srcdc = win32ui.CreateDCFromHandle(hwindc) | |
| memdc = srcdc.CreateCompatibleDC() | |
| bmp = win32ui.CreateBitmap() | |
| bmp.CreateCompatibleBitmap(srcdc, width, height) | |
| memdc.SelectObject(bmp) | |
| memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY) | |
| signedIntsArray = bmp.GetBitmapBits(True) | |
| img = np.fromstring(signedIntsArray, dtype='uint8') | |
| img.shape = (height,width,4) | |
| srcdc.DeleteDC() | |
| memdc.DeleteDC() | |
| win32gui.ReleaseDC(hwin, hwindc) | |
| win32gui.DeleteObject(bmp.GetHandle()) | |
| return cv2.cvtColor(img, cv2.COLOR_RGBA2RGB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment