import win32con, win32gui
from PIL import ImageGrab
import os
import time
import pyautogui
# 获取窗口位置 & 句柄
def get_window_pos(name):
name = name
handle = win32gui.FindWindow(0, name)
# 获取窗口句柄
if handle == 0:
return None
else:
# 返回坐标值和handle
return win32gui.GetWindowRect(handle), handle
# 截取 & 保存
def cut_save(save_dir,target_nums):
# 获取窗口坐标 & 句柄
(x1, y1, x2, y2), handle = get_window_pos('要截取的窗口的窗口标题')
# 发送还原最小化窗口的信息
win32gui.SendMessage(handle, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0)
# 将目标窗口移到最前面
win32gui.SetForegroundWindow(handle)
# 计数器
count = 1
while (count <= target_nums):
# 截图
img_ready = ImageGrab.grab((x1, y1, x2, y2))
# 保存图片
img_ready.save(os.path.join(save_dir, str(count) + '.png'))
count = count + 1
pyautogui.moveTo(1164, 625)# 将光标移动到1164, 625
pyautogui.dragTo(760, 625, 0.5, button='left') # 按住鼠标左键,用0.5s将鼠标拖拽到760, 625
time.sleep(0.1)# 延时0.1s
if __name__ == '__main__':
# 保存路径,截取数量
cut_save(r'保存路径',截取数量)