Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux grabscreen.py Python win32api_Linux_Win32gui - Fatal编程技术网

Linux grabscreen.py Python win32api

Linux grabscreen.py Python win32api,linux,win32gui,Linux,Win32gui,Win32gui或此代码是否有Linux或Mac OS等效库? 在一个外部项目上工作,这个windows代码将帮助我抓住屏幕。我还没有找到任何类似的库。多谢各位 def grab_screen(region=None): hwin = win32gui.GetDesktopWindow() if region: left,top,x2,y2 = region width = x2 - left + 1 height = y2 - top + 1

Win32gui或此代码是否有Linux或Mac OS等效库? 在一个外部项目上工作,这个windows代码将帮助我抓住屏幕。我还没有找到任何类似的库。多谢各位

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_BGRA2RGB)

您可以使用
pyautogui
抓取屏幕:

import pyautogui
image = pyautogui.screenshot('filename.png')
您可以这样做:) 我认为Mac OS不能使用WiIn32gui库。 你们可以用枕头来抓屏幕。 屏幕大小可以根据您想要的大小进行更改

import cv2
import numpy as np
import pyautogui
from PIL import ImageGrab

screen_w = 1920
screen_h = 1080

while True:
    rgb = ImageGrab.grab(bbox=(0, 0, screen_w, screen_h)) 
    rgb = np.array(rgb)

    cv2.imshow('window_frame', rgb)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


pip安装枕头
,显然!请不要在没有解释的情况下发布代码作为答案。试着解释一下你的代码是做什么的,以及它是如何解决问题的。带解释的答案通常更有帮助,质量也更好,更有可能吸引更多的选票。我添加了评论!