Skip to content
This repository has been archived by the owner on Sep 23, 2019. It is now read-only.

Commit

Permalink
FIXED GRABSCREEN
Browse files Browse the repository at this point in the history
New template
  • Loading branch information
jpvolt committed Jul 26, 2018
1 parent 036cfd9 commit 7d1b743
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions SVFBFuncs/grabscreen.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Done by Frannecklp

import cv2
import numpy as np
import win32gui, win32ui, win32con, win32api

from ctypes import windll

def grab_screen(region):

Expand All @@ -16,10 +14,10 @@ def grab_screen(region):

else:

hwin = win32gui.FindWindow(None, "Stardew Valley")
hwnd = win32gui.FindWindow(None, "Stardew Valley")

if hwin:
rect = win32gui.GetWindowRect(hwin)
if hwnd:
rect = win32gui.GetWindowRect(hwnd)

# If any dimension is negative the game is minimized, so we need to get the resolution from save file
if any(i < 0 for i in rect):
Expand All @@ -34,9 +32,30 @@ def grab_screen(region):
else:
print("game running")

left, top = rect[0:2]
width = rect[2] - rect[0]
height = rect[3] - rect[1]
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

saveDC.SelectObject(saveBitMap)

result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)

bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)

img = np.fromstring(bmpstr, dtype='uint8')
img.shape = (bmpinfo['bmHeight'], bmpinfo['bmWidth'], 4)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
return cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)

else:
print("game not running")
Expand All @@ -46,21 +65,5 @@ def grab_screen(region):
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_BGRA2GRAY)
Binary file added media/Images/frnew -4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7d1b743

Please sign in to comment.