wgc-python 2.0.0__tar.gz → 2.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wgc_python-2.0.1/PKG-INFO +717 -0
- wgc_python-2.0.1/README.md +688 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/pyproject.toml +1 -1
- wgc_python-2.0.1/wgc_python.egg-info/PKG-INFO +717 -0
- wgc_python-2.0.0/PKG-INFO +0 -372
- wgc_python-2.0.0/README.md +0 -343
- wgc_python-2.0.0/wgc_python.egg-info/PKG-INFO +0 -372
- {wgc_python-2.0.0 → wgc_python-2.0.1}/LICENSE +0 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/setup.cfg +0 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/wgc_python/__init__.py +0 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/wgc_python/wgc_python.dll +0 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/wgc_python.egg-info/SOURCES.txt +0 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/wgc_python.egg-info/dependency_links.txt +0 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/wgc_python.egg-info/requires.txt +0 -0
- {wgc_python-2.0.0 → wgc_python-2.0.1}/wgc_python.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: wgc_python
|
|
3
|
+
Version: 2.0.1
|
|
4
|
+
Summary: Windows Graphics Capture 窗口捕获库 — BGRA numpy 帧、按需捕获、零拷贝 GPU 路径
|
|
5
|
+
Author: XuanChenxuan
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/XuanChenxuan/wgc_python
|
|
8
|
+
Project-URL: Repository, https://github.com/XuanChenxuan/wgc_python
|
|
9
|
+
Project-URL: BugTracker, https://github.com/XuanChenxuan/wgc_python/issues
|
|
10
|
+
Keywords: wgc,windows-graphics-capture,screen-capture,automation,game-capture
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Multimedia :: Graphics :: Capture
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Requires-Python: >=3.8
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: numpy
|
|
28
|
+
Requires-Dist: opencv-python
|
|
29
|
+
|
|
30
|
+
# wgc_python
|
|
31
|
+
|
|
32
|
+
[English](#english) | 简体中文
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/wgc-python/)
|
|
35
|
+
[](https://pypi.org/project/wgc-python/)
|
|
36
|
+
[](LICENSE)
|
|
37
|
+
|
|
38
|
+
> **🚀 为 Python 自动化而生的窗口捕获库**
|
|
39
|
+
> 高帧率捕获 · 零资源待机 · 无视遮挡 · API 极简
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## 为什么选择 wgc_python?
|
|
44
|
+
|
|
45
|
+
### 🎯 专为自动化场景设计
|
|
46
|
+
|
|
47
|
+
你是否在为以下问题困扰?
|
|
48
|
+
|
|
49
|
+
- **mss/BitBlt**:无法捕获被遮挡或后台窗口
|
|
50
|
+
- **PrintWindow**:性能瓶颈,固定 26ms+ 延迟
|
|
51
|
+
- **其他 WGC 封装**:持续运行占用资源,频繁启停开销巨大(50ms+)
|
|
52
|
+
|
|
53
|
+
**wgc_python 通过 Pause/Resume 机制解决了这个矛盾:**
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
# 传统方式:要么持续空转浪费资源,要么频繁启停承受延迟
|
|
57
|
+
start_capture() # 50ms 开销
|
|
58
|
+
get_frame() # 获取截图
|
|
59
|
+
stop_capture() # 销毁会话(50ms)
|
|
60
|
+
# 下次截图又要重新开始...
|
|
61
|
+
|
|
62
|
+
# wgc_python 方式:一次启动,按需截图,零开销待机
|
|
63
|
+
with WindowCapture("窗口", "类名") as cap:
|
|
64
|
+
while running:
|
|
65
|
+
frame = cap.capture_one() # auto Resume → 等待帧 → 拷贝 → Pause
|
|
66
|
+
# 处理图像...
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 📊 性能对比
|
|
70
|
+
|
|
71
|
+
| 方案 | FPS | 后台捕获 | CPU 占用 | 频繁切换开销 | 暂停后 GPU 占用 |
|
|
72
|
+
|------|-----|---------|---------|-------------|----------------|
|
|
73
|
+
| python-mss / BitBlt | ~60 | ❌ | 高 | 低 | N/A (无暂停概念) |
|
|
74
|
+
| PrintWindow | ~38 | ✅ | 中 | 低 | N/A (每次调用即捕获) |
|
|
75
|
+
| 其他 WGC 封装 | 高 | ✅ | 高(持续空转) | 高 (启停会话开销大) | 高 (无法真正暂停) |
|
|
76
|
+
| **wgc_python** | **高** | ✅ | **极低(Pause时归零)** | **极低(原子标志位)** | **归零(无 D3D 操作)** |
|
|
77
|
+
|
|
78
|
+
> 表中为定性对比,具体数值因硬件、窗口内容与场景而异,建议以自己的实测为准。
|
|
79
|
+
|
|
80
|
+
### ✨ 核心优势
|
|
81
|
+
|
|
82
|
+
#### 1. 高帧率
|
|
83
|
+
- WGC 直接捕获 GPU 合成输出,不逐帧截屏,帧率上限远高于 PrintWindow 等 GDI 方案
|
|
84
|
+
- **双缓冲 Staging 纹理**:GPU 异步拷贝,读写互不阻塞
|
|
85
|
+
- **零拷贝友好**:`np.ndarray(strides=...)` 直接从 GPU 映射内存构造视图
|
|
86
|
+
|
|
87
|
+
#### 2. 智能资源管理
|
|
88
|
+
- **Pause/Resume 软暂停**:不销毁不重建 WGC session,仅原子标志位跳过帧处理
|
|
89
|
+
- **capture_one() 自动管理**:Resume → 等待帧 → 拷贝 → Pause,间隙 GPU 驱动零开销
|
|
90
|
+
- **会话复用**:避免频繁创建/销毁 D3D 设备的开销
|
|
91
|
+
|
|
92
|
+
#### 3. 极简 API
|
|
93
|
+
- **capture_one()**:一行代码完成按需捕获,返回 numpy 数组
|
|
94
|
+
- **get_frame()**:零拷贝裸指针路径(高级使用)
|
|
95
|
+
- **线程安全**:C++ 层处理所有多线程复杂性
|
|
96
|
+
|
|
97
|
+
#### 4. 多开并发
|
|
98
|
+
- 同一进程内可同时创建多个捕获会话,互不干扰
|
|
99
|
+
- 每个会话独立 D3D11 设备 + 独立纹理 + 独立 WinRT session,完全隔离
|
|
100
|
+
- 支持同窗口多路并发捕获
|
|
101
|
+
|
|
102
|
+
#### 5. 客户区精准裁剪(默认不截取标题栏/边框)
|
|
103
|
+
- **默认 `client_area_only=True`**:只捕获窗口客户区内容,自动裁剪标题栏和边框,直接输出有效像素
|
|
104
|
+
- **设置 `client_area_only=False`**:捕获整个窗口(含标题栏和边框),满足 UI 记录场景
|
|
105
|
+
- DPI 感知:自动修正高 DPI 缩放偏移,裁剪精度像素级
|
|
106
|
+
- GPU 级裁剪:`CopySubresourceRegion` 在 GPU 上完成裁剪,不浪费带宽和 CPU
|
|
107
|
+
|
|
108
|
+
#### 6. 光标捕获开关
|
|
109
|
+
- **默认 `capture_cursor=True`**:画面包含鼠标光标,与常规录屏行为一致
|
|
110
|
+
- **设置 `capture_cursor=False`**:画面不含鼠标指针,适合自动化 / 数据采集场景(也可用 `set_cursor_capture_enabled()` 运行时切换)
|
|
111
|
+
- 需 Windows 10 2004 (19041) 及以上系统,旧系统自动忽略该选项
|
|
112
|
+
|
|
113
|
+
#### 7. 无视遮挡
|
|
114
|
+
- 支持捕获被遮挡、最小化、后台窗口
|
|
115
|
+
- 完美适配游戏、桌面应用等各种场景
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 快速开始
|
|
120
|
+
|
|
121
|
+
### 安装
|
|
122
|
+
|
|
123
|
+
已发布至 PyPI,直接 pip 安装即可:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pip install wgc_python
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 基础用法
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
from wgc_python import WindowCapture, enumerate_windows
|
|
133
|
+
|
|
134
|
+
# 枚举所有窗口
|
|
135
|
+
for title, class_name in enumerate_windows():
|
|
136
|
+
print(f"{title} ({class_name})")
|
|
137
|
+
|
|
138
|
+
# 按需捕获(推荐 —— 零开销待机)
|
|
139
|
+
with WindowCapture("窗口标题", "窗口类名") as cap:
|
|
140
|
+
frame = cap.capture_one() # BGRA numpy 数组,shape (h, w, 4)
|
|
141
|
+
if frame is not None:
|
|
142
|
+
print(f"捕获成功: {frame.shape}")
|
|
143
|
+
|
|
144
|
+
# 客户区裁剪演示
|
|
145
|
+
# 默认 client_area_only=True:只截取客户区,不含标题栏/边框
|
|
146
|
+
cap_client = WindowCapture("记事本", "Notepad") # 只截内容
|
|
147
|
+
cap_full = WindowCapture("记事本", "Notepad", client_area_only=False) # 含标题栏
|
|
148
|
+
frame_client = cap_client.capture_one() # 只有编辑区
|
|
149
|
+
frame_full = cap_full.capture_one() # 含标题栏 + 菜单 + 编辑区
|
|
150
|
+
cap_client.close()
|
|
151
|
+
cap_full.close()
|
|
152
|
+
|
|
153
|
+
# 不捕获鼠标光标(默认 capture_cursor=True,保持旧版行为)
|
|
154
|
+
cap = WindowCapture("记事本", "Notepad", capture_cursor=False)
|
|
155
|
+
frame = cap.capture_one() # 画面不含鼠标指针
|
|
156
|
+
cap.set_cursor_capture_enabled(True) # 也支持运行时切换
|
|
157
|
+
cap.close()
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### 自动化最佳实践
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from wgc_python import WindowCapture
|
|
164
|
+
|
|
165
|
+
cap = WindowCapture("游戏窗口", "UnityWndClass")
|
|
166
|
+
|
|
167
|
+
while True:
|
|
168
|
+
frame = cap.capture_one(timeout=1.0)
|
|
169
|
+
if frame is not None:
|
|
170
|
+
# frame 是 BGRA numpy 数组,直接用于 OpenCV/模板匹配
|
|
171
|
+
pass
|
|
172
|
+
time.sleep(1)
|
|
173
|
+
|
|
174
|
+
cap.close()
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 零拷贝高级用法
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from wgc_python import WindowCapture
|
|
181
|
+
import numpy as np
|
|
182
|
+
import ctypes
|
|
183
|
+
|
|
184
|
+
with WindowCapture("窗口", "类名") as cap:
|
|
185
|
+
cap.resume()
|
|
186
|
+
r = cap.get_frame() # (ptr, w, h, row_pitch) — GPU 映射裸指针
|
|
187
|
+
if r:
|
|
188
|
+
ptr, w, h, rp = r
|
|
189
|
+
arr = np.ndarray((h, w, 4), dtype=np.uint8,
|
|
190
|
+
buffer=(ctypes.c_ubyte * (h * rp)).from_address(ptr),
|
|
191
|
+
strides=(rp, 4, 1))
|
|
192
|
+
# arr 是 GPU 内存的零拷贝视图
|
|
193
|
+
cap.release_frame()
|
|
194
|
+
cap.pause()
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### 实时显示
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from wgc_python import WindowCapture
|
|
201
|
+
import cv2
|
|
202
|
+
|
|
203
|
+
with WindowCapture("窗口标题", "窗口类名") as cap:
|
|
204
|
+
while True:
|
|
205
|
+
frame = cap.capture_one()
|
|
206
|
+
if frame is not None:
|
|
207
|
+
cv2.imshow("Capture", cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR))
|
|
208
|
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
209
|
+
break
|
|
210
|
+
cv2.destroyAllWindows()
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## API 参考
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
from wgc_python import (
|
|
219
|
+
WindowCapture, # 窗口捕获类(上下文管理器支持)
|
|
220
|
+
enumerate_windows, # 枚举所有可见窗口
|
|
221
|
+
get_last_error, # 获取最后错误信息(线程安全)
|
|
222
|
+
get_active_capture_count, # 获取活跃捕获数
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# WindowCapture 类方法:
|
|
226
|
+
# cap = WindowCapture(title, class_name, client_area_only=True, capture_cursor=True)
|
|
227
|
+
#
|
|
228
|
+
# cap.capture_one(timeout=0.5) -> np.ndarray | None ★ 推荐
|
|
229
|
+
# 自动 Resume → 等待帧 → 拷贝为 numpy → Pause
|
|
230
|
+
# 捕获间隙 WGC 完全休眠,GPU 驱动零开销
|
|
231
|
+
#
|
|
232
|
+
# cap.get_frame() -> (ptr, w, h, row_pitch) | None
|
|
233
|
+
# cap.release_frame() # 释放 GPU 映射
|
|
234
|
+
# cap.pause() # 暂停捕获(零资源待机)
|
|
235
|
+
# cap.resume() # 恢复捕获
|
|
236
|
+
# cap.set_cursor_capture_enabled(enabled) # 运行时切换光标捕获
|
|
237
|
+
# cap.stop() # 停止帧到达
|
|
238
|
+
# cap.close() # 销毁会话
|
|
239
|
+
# cap.is_capturing() -> bool
|
|
240
|
+
# cap.is_paused() -> bool
|
|
241
|
+
# cap.get_frame_count() -> int
|
|
242
|
+
# cap.handle -> int (DLL handle)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## 技术架构
|
|
248
|
+
|
|
249
|
+
```
|
|
250
|
+
WGC捕获 → GPU Surface纹理
|
|
251
|
+
│
|
|
252
|
+
┌────────▼────────┐
|
|
253
|
+
│ FrameArrived │
|
|
254
|
+
│ if pausing → ↑ │ ← Pause时直接返回,零 D3D 操作
|
|
255
|
+
└────────┬─────────┘
|
|
256
|
+
│
|
|
257
|
+
CopyResource (GPU异步复制)
|
|
258
|
+
↓
|
|
259
|
+
┌─────────────────────────┐
|
|
260
|
+
│ 双缓冲Staging纹理 │
|
|
261
|
+
│ [0] 写入 ←→ [1] 读取 │
|
|
262
|
+
│ m_textureInUse 防冲撞 │
|
|
263
|
+
└─────────────────────────┘
|
|
264
|
+
↓
|
|
265
|
+
Map (永久映射 GPU 内存)
|
|
266
|
+
↓
|
|
267
|
+
┌────── 零拷贝输出 ───────┐
|
|
268
|
+
│ get_frame() │
|
|
269
|
+
│ 返回裸指针 → numpy零拷贝 │
|
|
270
|
+
│ 需手动 release_frame() │
|
|
271
|
+
└──────────────────────────┘
|
|
272
|
+
|
|
273
|
+
┌────── 一键捕获 ──────────┐
|
|
274
|
+
│ capture_one() │
|
|
275
|
+
│ auto Pause/Resume │
|
|
276
|
+
│ 返回 numpy 数组 │
|
|
277
|
+
│ 间隙 GPU 驱动零开销 │
|
|
278
|
+
└──────────────────────────┘
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Pause/Resume 工作原理
|
|
282
|
+
|
|
283
|
+
```
|
|
284
|
+
用户调用 cap.pause()
|
|
285
|
+
│
|
|
286
|
+
m_isPaused = true ◄──── 原子标志位,微秒级
|
|
287
|
+
m_readableStagingIndex = -1
|
|
288
|
+
│
|
|
289
|
+
┌────▼────────────────────────────────────────────┐
|
|
290
|
+
│ FrameArrived 回调(WGC 仍会触发) │
|
|
291
|
+
│ │
|
|
292
|
+
│ lock(mutex); │
|
|
293
|
+
│ if (m_isPaused) return; // ← 纯CPU判断,跳过│
|
|
294
|
+
│ // ↓ 以下只在 resume 后执行 ↓ │
|
|
295
|
+
│ CopyResource(staging, frame); │
|
|
296
|
+
│ m_readableStagingIndex = idx; │
|
|
297
|
+
│ unlock(mutex); │
|
|
298
|
+
└────▲────────────────┬───────────────────────────┘
|
|
299
|
+
│ │
|
|
300
|
+
用户调用 cap.resume() MapFrame 检查 readableStagingIndex
|
|
301
|
+
m_isPaused = false <0 → 最近帧尚未就绪,返回 false
|
|
302
|
+
|
|
303
|
+
不销毁 WGC session / 不重建 D3D 设备 / 不重新注册回调
|
|
304
|
+
→ 恢复零延迟,无突刺
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## 文件结构
|
|
310
|
+
|
|
311
|
+
```
|
|
312
|
+
wgc_python/
|
|
313
|
+
├── wgc_python/ # Python 包
|
|
314
|
+
│ ├── __init__.py # Python API(ctypes FFI)
|
|
315
|
+
│ └── wgc_python.dll # 编译后的 DLL
|
|
316
|
+
├── wgc_python_dll/ # C++ DLL 项目
|
|
317
|
+
│ ├── WGCWindowCapture.h/cpp # WGC 捕获核心(双缓冲 + 零拷贝)
|
|
318
|
+
│ ├── WGCExport.h/cpp # DLL 导出(含线程安全错误处理)
|
|
319
|
+
│ ├── D3DInterop.cpp # D3D11 设备互操作
|
|
320
|
+
│ ├── WindowEnumerator.h/cpp # 窗口枚举
|
|
321
|
+
│ ├── pch.h # 预编译头
|
|
322
|
+
│ └── packages/ # NuGet 包
|
|
323
|
+
├── test.py # 功能测试
|
|
324
|
+
├── demon.py # 多线程实时显示示例
|
|
325
|
+
├── pyproject.toml # pip 构建配置
|
|
326
|
+
├── BUILD.md / BUILD_EN.md # 构建说明(中/英)
|
|
327
|
+
├── README.md / README_EN.md # 使用文档(中/英)
|
|
328
|
+
├── CONTRIBUTING.md # 贡献指南
|
|
329
|
+
├── CODE_OF_CONDUCT.md # 行为准则
|
|
330
|
+
├── LICENSE # MIT 许可证
|
|
331
|
+
└── requirements.txt # Python 依赖
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
---
|
|
335
|
+
|
|
336
|
+
## 系统要求
|
|
337
|
+
|
|
338
|
+
- Windows 10 1903+ (Build 18362),光标捕获开关需 2004+ (Build 19041)
|
|
339
|
+
- Python 3.8+
|
|
340
|
+
|
|
341
|
+
---
|
|
342
|
+
|
|
343
|
+
## 构建 DLL
|
|
344
|
+
|
|
345
|
+
详见 [BUILD.md](BUILD.md)
|
|
346
|
+
|
|
347
|
+
---
|
|
348
|
+
|
|
349
|
+
## 故障排除
|
|
350
|
+
|
|
351
|
+
| 问题 | 解决方案 |
|
|
352
|
+
|------|---------|
|
|
353
|
+
| DLL 未找到 | 确保 `wgc_python.dll` 在正确位置 |
|
|
354
|
+
| 捕获失败 | 检查窗口是否可见,Windows 版本 >= 1903 |
|
|
355
|
+
| 中文路径保存失败 | 使用 `cv2.imencode` + `open().write()` 代替 `cv2.imwrite` |
|
|
356
|
+
| 依赖缺失 | `pip install numpy opencv-python` |
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
## 适用场景
|
|
361
|
+
|
|
362
|
+
- ✅ 游戏 AI / 自动化脚本
|
|
363
|
+
- ✅ RPA 流程自动化
|
|
364
|
+
- ✅ 屏幕录制 / 直播
|
|
365
|
+
- ✅ UI 自动化测试
|
|
366
|
+
- ✅ 计算机视觉应用
|
|
367
|
+
|
|
368
|
+
---
|
|
369
|
+
|
|
370
|
+
## 鸣谢
|
|
371
|
+
|
|
372
|
+
本项目基于 [robmikh/Win32CaptureSample](https://github.com/robmikh/Win32CaptureSample) 开发。
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
## License
|
|
377
|
+
|
|
378
|
+
MIT License
|
|
379
|
+
|
|
380
|
+
---
|
|
381
|
+
|
|
382
|
+
<a id="english"></a>
|
|
383
|
+
## English
|
|
384
|
+
|
|
385
|
+
### 🎯 Designed for Automation Scenarios
|
|
386
|
+
|
|
387
|
+
Are you struggling with these problems?
|
|
388
|
+
|
|
389
|
+
- **mss/BitBlt**: Cannot capture occluded or background windows
|
|
390
|
+
- **PrintWindow**: Performance bottleneck, fixed 26ms+ latency
|
|
391
|
+
- **Other WGC wrappers**: Continuous resource consumption, huge overhead for frequent start/stop (50ms+)
|
|
392
|
+
|
|
393
|
+
**wgc_python solves this dilemma with its Pause/Resume mechanism:**
|
|
394
|
+
|
|
395
|
+
```python
|
|
396
|
+
# Traditional approach: Either waste resources or suffer latency
|
|
397
|
+
start_capture() # 50ms overhead
|
|
398
|
+
get_frame() # Get screenshot
|
|
399
|
+
stop_capture() # Destroy session (50ms)
|
|
400
|
+
|
|
401
|
+
# wgc_python approach: One-time init, on-demand capture, zero-overhead standby
|
|
402
|
+
with WindowCapture("Window", "Class") as cap:
|
|
403
|
+
while running:
|
|
404
|
+
frame = cap.capture_one() # auto Resume → wait → copy → Pause
|
|
405
|
+
# Process image...
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### 📊 Performance Comparison
|
|
409
|
+
|
|
410
|
+
| Solution | FPS | Background Capture | CPU Usage | Toggle Overhead | GPU When Paused |
|
|
411
|
+
|----------|-----|-------------------|-----------|----------------|-----------------|
|
|
412
|
+
| python-mss / BitBlt | ~60 | ❌ | High | Low | N/A (no pause) |
|
|
413
|
+
| PrintWindow | ~38 | ✅ | Medium | Low | N/A (per-call) |
|
|
414
|
+
| Other WGC wrappers | High | ✅ | High (continuous) | High (session start/stop) | High (can't truly pause) |
|
|
415
|
+
| **wgc_python** | **High** | ✅ | **Near Zero (when paused)** | **Very Low (atomic flag)** | **Zero (no D3D ops)** |
|
|
416
|
+
|
|
417
|
+
> Qualitative comparison only; actual numbers vary by hardware, window content, and workload — benchmark on your own setup.
|
|
418
|
+
|
|
419
|
+
### ✨ Core Advantages
|
|
420
|
+
|
|
421
|
+
#### 1. High Frame Rate
|
|
422
|
+
- WGC captures GPU composition output directly instead of per-frame GDI screenshots — frame rate ceiling far above PrintWindow-style approaches
|
|
423
|
+
- **Double-buffered Staging Texture**: GPU async copy, read/write non-blocking
|
|
424
|
+
- **Zero-copy path**: `np.ndarray(strides=...)` directly from GPU-mapped memory
|
|
425
|
+
|
|
426
|
+
#### 2. Smart Resource Management
|
|
427
|
+
- **Pause/Resume soft-pause**: Atomic flag only, no WGC session teardown
|
|
428
|
+
- **capture_one() auto management**: Resume → wait → copy → Pause, zero GPU driver overhead between captures
|
|
429
|
+
- **Session Reuse**: No frequent D3D device creation/destruction
|
|
430
|
+
|
|
431
|
+
#### 3. Minimalist API
|
|
432
|
+
- **capture_one()**: One-line on-demand capture, returns numpy array
|
|
433
|
+
- **get_frame()**: Zero-copy raw pointer path (advanced)
|
|
434
|
+
- **Thread safe**: C++ handles all multi-threading complexity
|
|
435
|
+
|
|
436
|
+
#### 4. Multi-Instance Capture
|
|
437
|
+
- Create multiple capture sessions simultaneously within one process
|
|
438
|
+
- Each session has its own D3D11 device, staging textures, and WinRT session — fully isolated
|
|
439
|
+
- Supports concurrent capture of the same window
|
|
440
|
+
|
|
441
|
+
#### 5. Client Area Precision (No Title Bar by Default)
|
|
442
|
+
- **Default `client_area_only=True`**: captures only window client area, automatically crops title bar and borders
|
|
443
|
+
- **Set `client_area_only=False`**: captures entire window including title bar and borders, for UI recording
|
|
444
|
+
- DPI-aware: automatic high-DPI scaling correction for pixel-perfect cropping
|
|
445
|
+
- GPU-level cropping via `CopySubresourceRegion`, no wasted bandwidth or CPU
|
|
446
|
+
|
|
447
|
+
#### 6. Cursor Capture Toggle
|
|
448
|
+
- **Default `capture_cursor=True`**: frame includes the mouse cursor, same as typical screen recording
|
|
449
|
+
- **Set `capture_cursor=False`**: frame excludes the mouse pointer, ideal for automation / data collection (also toggleable at runtime via `set_cursor_capture_enabled()`)
|
|
450
|
+
- Requires Windows 10 2004 (19041) or later; silently ignored on older systems
|
|
451
|
+
|
|
452
|
+
#### 7. Capture Behind Windows
|
|
453
|
+
- Supports capturing occluded, minimized, and background windows
|
|
454
|
+
- Perfect for games, desktop apps, and various scenarios
|
|
455
|
+
|
|
456
|
+
---
|
|
457
|
+
|
|
458
|
+
## Quick Start
|
|
459
|
+
|
|
460
|
+
### Installation
|
|
461
|
+
|
|
462
|
+
Published on PyPI — install directly with pip:
|
|
463
|
+
|
|
464
|
+
```bash
|
|
465
|
+
pip install wgc_python
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
### Basic Usage
|
|
469
|
+
|
|
470
|
+
```python
|
|
471
|
+
from wgc_python import WindowCapture, enumerate_windows
|
|
472
|
+
|
|
473
|
+
# Enumerate all windows
|
|
474
|
+
for title, class_name in enumerate_windows():
|
|
475
|
+
print(f"{title} ({class_name})")
|
|
476
|
+
|
|
477
|
+
# On-demand capture (recommended — zero-resource standby)
|
|
478
|
+
with WindowCapture("Window Title", "WindowClass") as cap:
|
|
479
|
+
frame = cap.capture_one() # BGRA numpy array, shape (h, w, 4)
|
|
480
|
+
if frame is not None:
|
|
481
|
+
print(f"Captured: {frame.shape}")
|
|
482
|
+
|
|
483
|
+
# Client area demo
|
|
484
|
+
# Default client_area_only=True: content only, no title bar/borders
|
|
485
|
+
cap_client = WindowCapture("Notepad", "Notepad") # content only
|
|
486
|
+
cap_full = WindowCapture("Notepad", "Notepad", client_area_only=False) # with title bar
|
|
487
|
+
frame_client = cap_client.capture_one() # edit area only
|
|
488
|
+
frame_full = cap_full.capture_one() # title bar + menu + edit area
|
|
489
|
+
cap_client.close()
|
|
490
|
+
cap_full.close()
|
|
491
|
+
|
|
492
|
+
# Disable mouse cursor capture (default capture_cursor=True, same as previous versions)
|
|
493
|
+
cap = WindowCapture("Notepad", "Notepad", capture_cursor=False)
|
|
494
|
+
frame = cap.capture_one() # frame does not contain the mouse pointer
|
|
495
|
+
cap.set_cursor_capture_enabled(True) # runtime toggle is also supported
|
|
496
|
+
cap.close()
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
### Best Practice for Automation
|
|
500
|
+
|
|
501
|
+
```python
|
|
502
|
+
from wgc_python import WindowCapture
|
|
503
|
+
|
|
504
|
+
cap = WindowCapture("Game Window", "UnityWndClass")
|
|
505
|
+
|
|
506
|
+
while True:
|
|
507
|
+
frame = cap.capture_one(timeout=1.0)
|
|
508
|
+
if frame is not None:
|
|
509
|
+
# frame is BGRA numpy array, ready for OpenCV/template matching
|
|
510
|
+
pass
|
|
511
|
+
time.sleep(1)
|
|
512
|
+
|
|
513
|
+
cap.close()
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
### Zero-Copy Advanced Usage
|
|
517
|
+
|
|
518
|
+
```python
|
|
519
|
+
from wgc_python import WindowCapture
|
|
520
|
+
import numpy as np
|
|
521
|
+
import ctypes
|
|
522
|
+
|
|
523
|
+
with WindowCapture("Window", "Class") as cap:
|
|
524
|
+
cap.resume()
|
|
525
|
+
r = cap.get_frame() # (ptr, w, h, row_pitch) — GPU mapped pointer
|
|
526
|
+
if r:
|
|
527
|
+
ptr, w, h, rp = r
|
|
528
|
+
arr = np.ndarray((h, w, 4), dtype=np.uint8,
|
|
529
|
+
buffer=(ctypes.c_ubyte * (h * rp)).from_address(ptr),
|
|
530
|
+
strides=(rp, 4, 1))
|
|
531
|
+
# arr is a zero-copy view into GPU-mapped memory
|
|
532
|
+
cap.release_frame()
|
|
533
|
+
cap.pause()
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
### Real-time Display
|
|
537
|
+
|
|
538
|
+
```python
|
|
539
|
+
from wgc_python import WindowCapture
|
|
540
|
+
import cv2
|
|
541
|
+
|
|
542
|
+
with WindowCapture("Window Title", "WindowClass") as cap:
|
|
543
|
+
while True:
|
|
544
|
+
frame = cap.capture_one()
|
|
545
|
+
if frame is not None:
|
|
546
|
+
cv2.imshow("Capture", cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR))
|
|
547
|
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
548
|
+
break
|
|
549
|
+
cv2.destroyAllWindows()
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
---
|
|
553
|
+
|
|
554
|
+
## API Reference
|
|
555
|
+
|
|
556
|
+
```python
|
|
557
|
+
from wgc_python import (
|
|
558
|
+
WindowCapture, # Window capture class (context manager support)
|
|
559
|
+
enumerate_windows, # Enumerate all visible windows
|
|
560
|
+
get_last_error, # Get last error message (thread-safe)
|
|
561
|
+
get_active_capture_count, # Get active capture count
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
# WindowCapture methods:
|
|
565
|
+
# cap = WindowCapture(title, class_name, client_area_only=True, capture_cursor=True)
|
|
566
|
+
#
|
|
567
|
+
# cap.capture_one(timeout=0.5) -> np.ndarray | None ★ recommended
|
|
568
|
+
# Auto Resume → wait for frame → copy to numpy → Pause
|
|
569
|
+
# WGC fully dormant between captures, zero GPU driver overhead
|
|
570
|
+
#
|
|
571
|
+
# cap.get_frame() -> (ptr, w, h, row_pitch) | None
|
|
572
|
+
# cap.release_frame() # Release GPU mapping
|
|
573
|
+
# cap.pause() # Pause capture (zero-resource standby)
|
|
574
|
+
# cap.resume() # Resume capture
|
|
575
|
+
# cap.set_cursor_capture_enabled(enabled) # Toggle cursor capture at runtime
|
|
576
|
+
# cap.stop() # Stop frame arrival
|
|
577
|
+
# cap.close() # Destroy session
|
|
578
|
+
# cap.is_capturing() -> bool
|
|
579
|
+
# cap.is_paused() -> bool
|
|
580
|
+
# cap.get_frame_count() -> int
|
|
581
|
+
# cap.handle -> int (DLL handle)
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
---
|
|
585
|
+
|
|
586
|
+
## Technical Architecture
|
|
587
|
+
|
|
588
|
+
```
|
|
589
|
+
WGC Capture → GPU Surface Texture
|
|
590
|
+
│
|
|
591
|
+
┌─────────▼──────────┐
|
|
592
|
+
│ FrameArrived │
|
|
593
|
+
│ if pausing → ↑ │ ← Paused: return directly, zero D3D ops
|
|
594
|
+
└─────────┬──────────┘
|
|
595
|
+
│
|
|
596
|
+
CopyResource (GPU async copy)
|
|
597
|
+
│
|
|
598
|
+
┌─────────────────────────────┐
|
|
599
|
+
│ Double-buffered Staging │
|
|
600
|
+
│ [0] Write ←→ [1] Read │
|
|
601
|
+
│ m_textureInUse anti-collide │
|
|
602
|
+
└─────────────────────────────┘
|
|
603
|
+
│
|
|
604
|
+
Map (permanently mapped GPU memory)
|
|
605
|
+
│
|
|
606
|
+
┌────── Zero-Copy Output ────┐
|
|
607
|
+
│ get_frame() │
|
|
608
|
+
│ raw ptr → numpy zero-copy │
|
|
609
|
+
│ requires release_frame() │
|
|
610
|
+
└─────────────────────────────┘
|
|
611
|
+
|
|
612
|
+
┌────── One-Click Capture ───┐
|
|
613
|
+
│ capture_one() │
|
|
614
|
+
│ auto Pause/Resume │
|
|
615
|
+
│ returns numpy array │
|
|
616
|
+
│ zero GPU overhead when idle │
|
|
617
|
+
└─────────────────────────────┘
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
### How Pause/Resume Works
|
|
621
|
+
|
|
622
|
+
```
|
|
623
|
+
User calls cap.pause()
|
|
624
|
+
│
|
|
625
|
+
m_isPaused = true ◄──── atomic flag, microsecond-level
|
|
626
|
+
m_readableStagingIndex = -1
|
|
627
|
+
│
|
|
628
|
+
┌────▼────────────────────────────────────────────┐
|
|
629
|
+
│ FrameArrived Callback (WGC still fires)│
|
|
630
|
+
│ │
|
|
631
|
+
│ lock(mutex); │
|
|
632
|
+
│ if (m_isPaused) return; // ← pure CPU, skip│
|
|
633
|
+
│ // ↓ below runs only after resume ↓ │
|
|
634
|
+
│ CopyResource(staging, frame); │
|
|
635
|
+
│ m_readableStagingIndex = idx; │
|
|
636
|
+
│ unlock(mutex); │
|
|
637
|
+
└────▲────────────────┬───────────────────────────┘
|
|
638
|
+
│ │
|
|
639
|
+
User calls cap.resume() MapFrame checks readableStagingIndex
|
|
640
|
+
m_isPaused = false <0 → no frame ready → returns false
|
|
641
|
+
|
|
642
|
+
No WGC session destroy / no D3D device recreate / no callback re-register
|
|
643
|
+
→ zero-latency resume, no spikes
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
---
|
|
647
|
+
|
|
648
|
+
## File Structure
|
|
649
|
+
|
|
650
|
+
```
|
|
651
|
+
wgc_python/
|
|
652
|
+
├── wgc_python/ # Python package
|
|
653
|
+
│ ├── __init__.py # Python API (ctypes FFI)
|
|
654
|
+
│ └── wgc_python.dll # Compiled DLL
|
|
655
|
+
├── wgc_python_dll/ # C++ DLL Project
|
|
656
|
+
│ ├── WGCWindowCapture.h/cpp # Capture core (double-buffered + zero-copy)
|
|
657
|
+
│ ├── WGCExport.h/cpp # DLL exports (thread-safe error handling)
|
|
658
|
+
│ ├── D3DInterop.cpp # D3D11 device interop
|
|
659
|
+
│ ├── WindowEnumerator.h/cpp # Window enumeration
|
|
660
|
+
│ ├── pch.h # Precompiled header
|
|
661
|
+
│ └── packages/ # NuGet packages
|
|
662
|
+
├── test.py # Functional tests
|
|
663
|
+
├── demon.py # Threaded realtime display example
|
|
664
|
+
├── pyproject.toml # pip build config
|
|
665
|
+
├── BUILD.md / BUILD_EN.md # Build instructions (CN/EN)
|
|
666
|
+
├── README.md / README_EN.md # Usage docs (CN/EN)
|
|
667
|
+
├── CONTRIBUTING.md # Contributing guide
|
|
668
|
+
├── CODE_OF_CONDUCT.md # Code of conduct
|
|
669
|
+
├── LICENSE # MIT License
|
|
670
|
+
└── requirements.txt # Python dependencies
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
---
|
|
674
|
+
|
|
675
|
+
## Build DLL
|
|
676
|
+
|
|
677
|
+
See [BUILD.md](BUILD.md)
|
|
678
|
+
|
|
679
|
+
---
|
|
680
|
+
|
|
681
|
+
## Requirements
|
|
682
|
+
|
|
683
|
+
- Windows 10 1903+ (Build 18362); cursor capture toggle requires 2004+ (Build 19041)
|
|
684
|
+
- Python 3.8+
|
|
685
|
+
|
|
686
|
+
---
|
|
687
|
+
|
|
688
|
+
## Troubleshooting
|
|
689
|
+
|
|
690
|
+
| Issue | Solution |
|
|
691
|
+
|-------|----------|
|
|
692
|
+
| DLL not found | Ensure `wgc_python.dll` is in the correct location |
|
|
693
|
+
| Capture failed | Check if window is visible, Windows version >= 1903 |
|
|
694
|
+
| Non-ASCII path save failed | Use `cv2.imencode` + `open().write()` instead of `cv2.imwrite` |
|
|
695
|
+
| Missing dependencies | `pip install numpy opencv-python` |
|
|
696
|
+
|
|
697
|
+
---
|
|
698
|
+
|
|
699
|
+
## Use Cases
|
|
700
|
+
|
|
701
|
+
- ✅ Game AI / Automation Scripts
|
|
702
|
+
- ✅ RPA Process Automation
|
|
703
|
+
- ✅ Screen Recording / Streaming
|
|
704
|
+
- ✅ UI Automation Testing
|
|
705
|
+
- ✅ Computer Vision Applications
|
|
706
|
+
|
|
707
|
+
---
|
|
708
|
+
|
|
709
|
+
## Acknowledgments
|
|
710
|
+
|
|
711
|
+
This project is based on [robmikh/Win32CaptureSample](https://github.com/robmikh/Win32CaptureSample).
|
|
712
|
+
|
|
713
|
+
---
|
|
714
|
+
|
|
715
|
+
## License
|
|
716
|
+
|
|
717
|
+
MIT License
|