Files
douyi_guaji_main/douyin/detection/pixel/_common.py
T

36 lines
991 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
def _safe_open_screenshot_rgb(pic_path: str):
try:
if not pic_path or (not os.path.exists(pic_path)):
return None
from PIL import Image
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