'commit'
This commit is contained in:
73
Util/DevTools/crop_test.py
Normal file
73
Util/DevTools/crop_test.py
Normal file
@@ -0,0 +1,73 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
基于行扫描和统计特征的卡片截取工具
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到系统路径
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.append(project_root)
|
||||
from Util import Kit
|
||||
|
||||
def clean_directory(dir_path):
|
||||
"""
|
||||
清理现场:删除目录下除纯数字.jpg以外的所有文件
|
||||
"""
|
||||
print(f"Cleaning directory: {dir_path}")
|
||||
if not os.path.exists(dir_path):
|
||||
return
|
||||
|
||||
for filename in os.listdir(dir_path):
|
||||
path = os.path.join(dir_path, filename)
|
||||
if not os.path.isfile(path):
|
||||
continue
|
||||
|
||||
name, ext = os.path.splitext(filename)
|
||||
# 保留条件:后缀是 .jpg 且文件名是纯数字
|
||||
# [修改] 同时保留 _flag.jpg, _vl.jpg 供查看
|
||||
# [修改] 保留 .json 文件
|
||||
if ext.lower() == ".json":
|
||||
continue
|
||||
|
||||
if ext.lower() == ".jpg" and (name.isdigit() or name.endswith("_flag") or name.endswith("_vl")):
|
||||
continue
|
||||
|
||||
# 清理旧的 _for_vl.jpg
|
||||
if name.endswith("_for_vl"):
|
||||
pass # Let it fall through to delete
|
||||
else:
|
||||
# 其他非生成的文件,可能需要保留吗?
|
||||
# 这里的逻辑是清理 output 目录,假设该目录下只有生成的图片
|
||||
# 如果是原始图片(比如 1.jpg),不能删!
|
||||
pass
|
||||
|
||||
# 原始图片保护 (文件名比较短,通常是 1.jpg, 2.jpg 等)
|
||||
if len(name) <= 2 and name.isdigit():
|
||||
continue
|
||||
|
||||
try:
|
||||
os.remove(path)
|
||||
print(f" Deleted: {filename}")
|
||||
except Exception as e:
|
||||
print(f" Error deleting {filename}: {e}")
|
||||
|
||||
def crop_cards(img_path):
|
||||
Kit.crop_cards_from_image(img_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_files = [
|
||||
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\1.jpg",
|
||||
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\2.jpg",
|
||||
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\3.jpg",
|
||||
r"d:\dsWork\dsProject\dsCrawler\Tools\Images\4.jpg"
|
||||
]
|
||||
|
||||
# 在测试前清理现场
|
||||
if test_files:
|
||||
# 假设所有图片都在同一个文件夹
|
||||
target_dir = os.path.dirname(test_files[0])
|
||||
clean_directory(target_dir)
|
||||
|
||||
for f in test_files:
|
||||
crop_cards(f)
|
||||
58
Util/DevTools/screenshot_test.py
Normal file
58
Util/DevTools/screenshot_test.py
Normal file
@@ -0,0 +1,58 @@
|
||||
# coding=utf-8
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime
|
||||
import uiautomator2 as u2
|
||||
|
||||
# 配置日志输出
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger("ScreenshotTool")
|
||||
|
||||
|
||||
async def take_screenshot():
|
||||
"""
|
||||
打开微信主界面并拍照(截图)
|
||||
"""
|
||||
# 连接设备
|
||||
d = u2.connect()
|
||||
|
||||
# 执行截图
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
filename = f"current_{timestamp}.jpg"
|
||||
|
||||
# 获取目标目录: 项目根目录下的 Temp/DevTools
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
target_dir = os.path.join(project_root, "Temp", "DevTools")
|
||||
os.makedirs(target_dir, exist_ok=True)
|
||||
filepath = os.path.join(target_dir, filename)
|
||||
|
||||
logger.info(f"正在拍照(截图)并保存至: {filepath}")
|
||||
d.screenshot(filepath)
|
||||
|
||||
if os.path.exists(filepath):
|
||||
logger.info("拍照成功!")
|
||||
return filepath
|
||||
else:
|
||||
logger.error("拍照失败,未找到生成的文件。")
|
||||
return None
|
||||
|
||||
|
||||
async def main():
|
||||
filepath = await take_screenshot()
|
||||
if filepath:
|
||||
logger.info(f"任务执行完成,截图文件: {filepath}")
|
||||
else:
|
||||
logger.error("任务执行失败。")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except KeyboardInterrupt:
|
||||
logger.info("程序被用户中断.")
|
||||
except Exception as e:
|
||||
logger.exception(f"运行过程中出现异常: {e}")
|
||||
Reference in New Issue
Block a user