由上一篇的思路我们可以定义以下的具体实现步骤
本篇将围绕窗口捕获、屏幕截图、截图切分讲述screen.py代码
环境描述C:\Users\SF>ver
Microsoft Windows [版本 10.0.18363.720]
C:\Users\SF>python --version
Python 3.7.6
启动方式 多标签版
分辨率 800x600
界面风格 暖风
窗口截图
主要是使用了PyQt5进行截图
hwnd_title = dict()
def get_all_hwnd(hwnd,mouse):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)})
def shot():
win32gui.EnumWindows(get_all_hwnd, 0)
mhxy_title = ''
for h,t in hwnd_title.items():
if t.startswith('梦幻西游 ONLINE'):
mhxy_title = t
print(mhxy_title)
hwnd = win32gui.FindWindow(None, mhxy_title)
app = QApplication(sys.argv)
desktop_id = app.desktop().winId()
screen= QApplication.primaryScreen()
img_desk = screen.grabWindow(desktop_id).toImage()
img_sc = screen.grabWindow(hwnd).toImage()
img_desk.save(img_desktop_path)
img_sc.save(img_sc_path)
print(f'img_desktop save to -> {os.path.abspath(img_desktop_path)}')
print(f'img_mhxy save to -> {os.path.abspath(img_sc_path)}')
if mhxy_title == '':
print('mhxy not start')
return False
return True
于是我们在路径
images
|- desktop.jpg
|- mhxy.jpg
得到一张全屏截图以及一张梦幻的窗口截图
截图切分
得到屏幕截图后,我