03handleshot.py****内容:
import time
import os
import configparser
import pywinauto
import pyautogui
"""
功能:前台截图
参数:class_name:窗口类别,title:窗口名称,模糊匹配
"""
def captureAsImage(class_name,title,box0,outputDirPath):
app=pywinauto.application.Application().connect(class_name=class_name,title_re=title)
hwnd = app.top_window() # 获取顶层窗口句柄
"""1、移动窗口至Z循序的最前端"""
hwnd_focus=hwnd.has_focus()
if hwnd_focus==False:
print("hwnd_focus:", hwnd_focus)
hwnd.set_focus()
time.sleep(1) # 窗口移植最前端需要时间
"""2、移动窗口至指定点位"""
hwnd_rect = hwnd.rectangle() # 获取窗口的位置,即宽度、高度
# print("hwnd_rect:",dir(hwnd_rect))
hwnd_left=hwnd_rect.left
hwnd_top=hwnd_rect.top
if box0[0]!=hwnd_left or box0[1]!=hwnd_top:
print("hwnd_left:", hwnd_rect.left)
print("hwnd_top:", hwnd_rect.top)
print("hwnd_width:", hwnd_rect.width()) # 展示窗口大小
print("hwnd_height:", hwnd_rect.height())
hwnd.move_window(x=box0[0], y=box0[1]) # 移动窗口至指定点位
time.sleep(1) # 窗口移植最前端需要时间
"""3、截图"""
img_PIL = pyautogui.screenshot(region=box0)
img_name=outputDirPath+"/img_"+time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time()))+".png"
print("save",img_name)
img_PIL.save(img_name,"PNG")
按天生成文件夹,并返回文件夹名称
def createDirFromTime():
dir_name="tmp_"+time.strftime('%Y.%m.%d', time.localtime(time.time()))
if os.path.exists(dir_name)==False:# 文件夹不存在创建
os.makedirs(dir_name)
print("create dir:",dir_name)
else:
print("dir",dir_name,"exist")
return dir_name
if name == "main":
print("start...")
cf = configparser.ConfigParser()
cf.read("03handleshot.ini",encoding='utf-8')
delay = int(cf.get("sec_a", "delay"))
interval=int(cf.get("sec_a", "interval"))
class_name = cf.get("sec_a", "class_name")
title = cf.get("sec_a", "title")
box = cf.get("sec_a", "box")
print("delay:",delay)
print("interval:", interval)
print("class_name:", class_name)
print("title:", title)
print("box:", box)
time.sleep(delay)
# 生成文件夹
dir_name = createDirFromTime()
for i in range(100000):
captureAsImage(class_name,title,eval(box),dir_name)
time.sleep(interval)
print("end...")
输入:03handleshot.ini内容:
[sec_a]
延迟多少后开始截图
delay = 10
两次截图的间隔
interval = 5
窗口的类
class_name = wxWindowNR
窗口的名称,正则化
title = 查询工具
窗口的位置,左上角,长宽
box = (300,200,1125,750)
5、打包
D:\MrWu\DCode\python312\hanzi02\pkg1>D:\MrWu\procdure\Apython\Scripts\pyinstaller -F 03handleshot.py
版权归原作者 m0_72280720 所有, 如有侵权,请联系我们删除。