我正在使用 MSS 捕获我的屏幕截图。 (因为它捕获更快的屏幕截图)
但我不确定如何在 Mac 中捕获特定窗口,我知道他们为 Windows 用户提供了 win32... 我现在拥有的代码只是一个不断循环捕获我的主监视器。
主要文件:
import cv2 as cv
import numpy as np
from time import time
from mss import mss
def window_capture():
loop_time = time()
with mss() as sct:
monitor = {"top": 40, "left": 0, "width": 800, "height": 600}
while(True):
screenshot = np.array(sct.grab(monitor))
screenshot = cv.cvtColor(screenshot, cv.COLOR_RGB2BGR)
cv.imshow('Computer Vision', screenshot)
print('FPS {}'.format(1 / (time() - loop_time)))
loop_time = time()
if cv.waitKey(1) == ord('q'):
cv.destroyAllWindows()
break
window_capture()
print('Done.')
最佳答案
我编写了以下 ObjectiveC
片段,它获取 macOS 中所有窗口的名称、所有者、窗口 ID 和在屏幕上的位置。我将其保存为 windowlist.m
并使用文件顶部注释中的命令对其进行了编译:
////////////////////////////////////////////////////////////////////////////////
// windowlist.m
// Mark Setchell
//
// Get list of windows with their characteristics
//
// Compile with:
// clang windowlist.m -o windowlist -framework coregraphics -framework cocoa
//
// Run with:
// ./windowlist
//
////////////////////////////////////////////////////////////////////////////////
#include <Cocoa/Cocoa.h>
#include <CoreGraphics/CGWindow.h>
int main(int argc, char **argv)
{
NSArray *windows = (NSArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly,kCGNullWindowID);
for(NSDictionary *window in windows){
int WindowNum = [[window objectForKey:(NSString *)kCGWindowNumber] intValue];
NSString* OwnerName = [window objectForKey:(NSString *)kCGWindowOwnerName];
int OwnerPID = [[window objectForKey:(NSString *) kCGWindowOwnerPID] intValue];
NSString* WindowName= [window objectForKey:(NSString *)kCGWindowName];
CFDictionaryRef bounds = (CFDictionaryRef)[window objectForKey:(NSString *)kCGWindowBounds];
CGRect rect;
CGRectMakeWithDictionaryRepresentation(bounds,&rect);
printf("%s:%s:%d:%d:%f,%f,%f,%f\n",[OwnerName UTF8String],[WindowName UTF8String],WindowNum,OwnerPID,rect.origin.x,rect.origin.y,rect.size.height,rect.size.width);
}
}
它给出这样的输出,其中每行的最后 4 项是窗口左上角、高度和宽度。您可以使用 Python 的 subprocess.Popen()
“按原样” 运行此程序并获取窗口列表,或者您可以使用 PyObjc 将其转换为 Python
Python 模块:
Location Menu:Item-0:4881:1886:1043.000000,0.000000,22.000000,28.000000
Backup and sync from Google:Item-0:1214:8771:1071.000000,0.000000,22.000000,30.000000
Dropbox:Item-0:451:1924:1101.000000,0.000000,22.000000,28.000000
NordVPN IKE:Item-0:447:1966:1129.000000,0.000000,22.000000,26.000000
PromiseUtilityDaemon:Item-0:395:1918:1155.000000,0.000000,22.000000,24.000000
SystemUIServer:AppleTimeMachineExtra:415:1836:1179.000000,0.000000,22.000000,40.000000
SystemUIServer:AppleBluetoothExtra:423:1836:1219.000000,0.000000,22.000000,30.000000
SystemUIServer:AirPortExtra:409:1836:1249.000000,0.000000,22.000000,30.000000
SystemUIServer:AppleVolumeExtra:427:1836:1279.000000,0.000000,22.000000,30.000000
SystemUIServer:BatteryExtra:405:1836:1309.000000,0.000000,22.000000,67.000000
SystemUIServer:AppleClockExtra:401:1836:1376.000000,0.000000,22.000000,123.000000
SystemUIServer:AppleUser:419:1836:1499.000000,0.000000,22.000000,99.000000
Spotlight:Item-0:432:1922:1598.000000,0.000000,22.000000,36.000000
SystemUIServer:NotificationCenter:391:1836:1634.000000,0.000000,22.000000,46.000000
Window Server:Menubar:353:253:0.000000,0.000000,22.000000,1680.000000
Dock:Dock:387:1835:0.000000,0.000000,1050.000000,1680.000000
Terminal:windowlist — -bash — 140×30:4105:6214:70.000000,285.000000,658.000000,1565.000000
关于python - 苹果操作系统 : Using python to capture screenshots of a specific window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62707662/