覆盖 14 个文件中的 Image.open() 未正确关闭问题: - fansclub_handler: 5 处(pic/pic_rgb/img 未 close) - image_ops: 2 处(crop 后 pic 未 close) - douyin_popups: 2 处(pic.convert 重赋值泄漏原始句柄) - douyin_fudai: 1 处(open().convert() 链式调用泄漏) - douyin_robot: 2 处(同上) - save_files/direct_attend/finder/stage/find_open/detail/_common/ocr_preprocess: 各 1-2 处 统一修复模式:分离 open 与 convert,try/finally 确保原始句柄关闭。 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
180 lines
4.9 KiB
Python
180 lines
4.9 KiB
Python
from __future__ import annotations
|
||
|
||
"""screenshot_manager.save_files - 双截图记录的保存接口。
|
||
|
||
实时操作截图(latest_attempt_<type>.png)每次都覆盖;
|
||
成功识别存档截图(last_success_<type>.png)仅在成功时保存。
|
||
"""
|
||
|
||
import os
|
||
from PIL import Image
|
||
|
||
from ._common import _mgr_log
|
||
from .paths import (
|
||
ensure_ocr_dir,
|
||
get_latest_attempt_path,
|
||
get_last_success_path,
|
||
get_ocr_dir,
|
||
)
|
||
|
||
|
||
def cleanup_ocr_dir(
|
||
keep_latest_attempt: bool = True,
|
||
keep_last_success: bool = True,
|
||
keep_success_files: bool = True,
|
||
max_files: int = 50,
|
||
) -> int:
|
||
"""清理OCR目录中的旧文件
|
||
|
||
参数:
|
||
keep_latest_attempt: 是否保留所有 latest_attempt_*.png 文件
|
||
keep_last_success: 是否保留所有 last_success_*.png 文件
|
||
keep_success_files: 是否保留所有 _success.png 文件
|
||
max_files: 最多保留的文件数(不包括特殊文件)
|
||
|
||
返回:
|
||
清理的文件数量
|
||
"""
|
||
try:
|
||
ocr_dir = get_ocr_dir()
|
||
if not os.path.exists(ocr_dir):
|
||
return 0
|
||
|
||
files = []
|
||
for f in os.listdir(ocr_dir):
|
||
file_path = os.path.join(ocr_dir, f)
|
||
if os.path.isfile(file_path) and f.lower().endswith(".png"):
|
||
# 检查是否为特殊文件
|
||
is_special = False
|
||
if f.startswith("latest_attempt_") and keep_latest_attempt:
|
||
is_special = True
|
||
elif f.startswith("last_success_") and keep_last_success:
|
||
is_special = True
|
||
elif f.endswith("_success.png") and keep_success_files:
|
||
is_special = True
|
||
|
||
if not is_special:
|
||
files.append((file_path, os.path.getmtime(file_path)))
|
||
|
||
# 按修改时间排序,保留最新的
|
||
files.sort(key=lambda x: x[1], reverse=True)
|
||
|
||
# 删除超出限制的文件
|
||
deleted_count = 0
|
||
for file_path, _ in files[max_files:]:
|
||
try:
|
||
os.remove(file_path)
|
||
deleted_count += 1
|
||
except Exception:
|
||
pass
|
||
|
||
return deleted_count
|
||
except Exception:
|
||
return 0
|
||
|
||
|
||
|
||
|
||
def save_latest_attempt(image: Image.Image, operation_type: str = "") -> bool:
|
||
"""保存实时操作截图(覆盖保存)
|
||
|
||
参数:
|
||
image: PIL Image 对象
|
||
operation_type: 操作类型(用于分类,如 "template_match", "pixel_detect", "ocr", "cut_pic")
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
ensure_ocr_dir()
|
||
path = get_latest_attempt_path(operation_type)
|
||
image.save(path, "PNG")
|
||
return True
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 保存 latest_attempt_{operation_type}.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
|
||
|
||
def save_last_success(image: Image.Image, operation_type: str = "") -> bool:
|
||
"""保存成功识别存档截图(不覆盖,仅成功时保存)
|
||
|
||
参数:
|
||
image: PIL Image 对象
|
||
operation_type: 操作类型(用于分类,如 "template_match", "pixel_detect", "ocr", "cut_pic")
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
ensure_ocr_dir()
|
||
path = get_last_success_path(operation_type)
|
||
image.save(path, "PNG")
|
||
return True
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 保存 last_success_{operation_type}.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
|
||
|
||
def save_attempt_from_file(source_path: str, operation_type: str = "") -> bool:
|
||
"""从文件保存实时操作截图
|
||
|
||
参数:
|
||
source_path: 源图片文件路径
|
||
operation_type: 操作类型
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
if not os.path.exists(source_path):
|
||
return False
|
||
_raw = Image.open(source_path)
|
||
try:
|
||
return save_latest_attempt(_raw, operation_type)
|
||
finally:
|
||
_raw.close()
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 从文件保存 latest_attempt.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
|
||
|
||
def save_success_from_file(source_path: str, operation_type: str = "") -> bool:
|
||
"""从文件保存成功识别存档截图
|
||
|
||
参数:
|
||
source_path: 源图片文件路径
|
||
operation_type: 操作类型
|
||
|
||
返回:
|
||
是否保存成功
|
||
"""
|
||
try:
|
||
if not os.path.exists(source_path):
|
||
return False
|
||
_raw = Image.open(source_path)
|
||
try:
|
||
return save_last_success(_raw, operation_type)
|
||
finally:
|
||
_raw.close()
|
||
except Exception as e:
|
||
try:
|
||
_mgr_log(f"[截图管理] 从文件保存 last_success.png 失败: {e}")
|
||
except Exception:
|
||
pass
|
||
return False
|