-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (62 loc) · 1.78 KB
/
Copy pathutils.py
File metadata and controls
73 lines (62 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from rich.console import Console
from rich.logging import RichHandler
import logging
import numpy as np
import cv2 as cv
import os
# 控制台对象
console = Console()
# 日志记录
logging.basicConfig(
format="%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.WARNING,
handlers=[
RichHandler(
console=console,
level=logging.NOTSET,
rich_tracebacks=True,
tracebacks_show_locals=True,
tracebacks_suppress=[],
tracebacks_max_frames=100,
)
],
force=False,
)
logger = logging.getLogger('VidPrismCLI')
logger.setLevel(logging.INFO)
def safe_imread(file: str) -> np.ndarray:
"""
支持非 ASCII 编码路径的图像读取
Args:
file (str): 要读取的图像路径
Returns:
np.ndarray: 读取的 BGR 图像
"""
# 以 uint8 数据类型,读取并解析二进制图像文件中的数据
buffer = np.fromfile(file, dtype=np.uint8)
# 解码数据为图像
image = cv.imdecode(buffer, cv.IMREAD_COLOR)
assert image is not None, f"Failed to load image: {file}"
return image
def safe_imwrite(file: str, image: np.ndarray) -> True:
"""
支持非 ASCII 编码路径的图像写入
Args:
file (str): 要写入的图像路径
image (np.ndarray): 要写入的图像
Returns:
True: 是否写入成功
"""
# 获取图像扩展名
root, ext = os.path.splitext(file)
# 将图像编码为指定文件格式的二进制数据
ret, buffer = cv.imencode(ext, image)
# 编码成功
if ret:
# 写入数据
buffer.tofile(file)
return True
# 编码失败
else:
return False