Files
douyi_guaji_main/douyin/detection/pixel/_common.py
T
CodexandClaude Sonnet 4.5 bb90e2983f fix(pil): 补充修复剩余 PIL Image 资源泄漏
覆盖 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>
2026-05-23 22:49:50 +08:00

36 lines
1008 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# pyright: reportGeneralTypeIssues=false
# pyright: reportAttributeAccessIssue=false
# pyright: reportMissingModuleSource=false
# pyright: reportUnknownMemberType=false
# pyright: reportUnknownVariableType=false
# pylint: disable=no-member
"""pixel._common - 共享工具函数。
只包含 _safe_open_screenshot_rgb:安全打开截图为 RGB Image,
处理 truncated/损坏文件。被 popups / buttons / stage 子模块共用。
"""
import os
from typing import Any, Optional
from PIL import Image
def _safe_open_screenshot_rgb(pic_path: str) -> Optional[Image.Image]:
try:
if not pic_path or (not os.path.exists(pic_path)):
return None
pic = Image.open(pic_path)
try:
pic.load()
return pic.convert("RGB")
finally:
pic.close()
except (OSError, IOError, SyntaxError):
# Handle truncated image files
return None
except Exception as e: # type: ignore[general-exception]
return None