-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtils.py
More file actions
139 lines (116 loc) · 3.53 KB
/
ImageUtils.py
File metadata and controls
139 lines (116 loc) · 3.53 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
from typing import NewType, Tuple
import cv2
import numpy as np
import base64
from utils.ScreenUtils import suitable_screensize
CvImage = NewType("CvImage", np.ndarray) # cv2 ImageObject(np.ndarray)screenUtils.py
def read_image(filepath: str) -> CvImage:
"""
cv2.imread() 的兼容中文路径的代替方法
:param filepath: 文件路径
:return: CvImage cv2图片
"""
return cv2.imdecode(np.fromfile(filepath, dtype=np.uint8), -1)
def save_image(image: CvImage, filepath: str) -> None:
"""
保存图片到文件
:param image: CvImage cv2图片
:param filepath: 文件路径
:return:
"""
name, ext = os.path.splitext(filepath)
if not ext:
ext = ".jpg"
with open(filepath, 'wb') as fp:
fp.write(cv2bytes(image, ext))
def bytes2cv(image: bytes) -> CvImage:
"""
二进制图片转cv2图片
:param image: bytesImage 二进制图片数据
:return: CvImage cv2图片
"""
return cv2.imdecode(np.array(bytearray(image), dtype='uint8'), cv2.IMREAD_UNCHANGED) # 从二进制图片数据中读取
def cv2bytes(image: CvImage, ext: str = ".jpg") -> bytes:
"""
cv2图片转二进制图片
:param image: CvImage cv2图片
:param ext: 图片格式
:return: bytesImage 二进制图片数据
"""
_, enc = cv2.imencode(ext, image)
return np.array(enc).tobytes()
def image_to_base64(image_np: CvImage) -> str:
"""
将cv2图片转码为base64格式
image_np: CvImage cv2图片
Returns: base64编码的数据
"""
image = cv2.imencode('.png', image_np)[1]
image_base64 = str(base64.b64encode(image))[2:-1]
return image_base64
def base64_to_image(base64_code: str) -> CvImage:
"""
将base64编码解析成cv2图片
base64_code: base64编码的数据
Returns: CvImage cv2图片
"""
# base64解码
img_data = base64.b64decode(base64_code)
# 转换为np数组
img_array = np.frombuffer(img_data, np.uint8)
# 转换成opencv可用格式
image = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)
return image
def image_size(image: CvImage) -> Tuple[int, int]:
"""
获取图片大小
:param image: CvImage cv2图片
:return: 图片大小元组
"""
return image.shape[:2][::-1]
def show(image: CvImage, title: str = 'Image', time: int = 0) -> None:
"""
显示图片
:param image: CvImage cv2图片
:param title: 窗口标题
:param time: 持续时间
:return:
"""
cv2.imshow(title, image)
cv2.waitKey(time)
cv2.destroyAllWindows()
def show_adapt(image: CvImage, title: str = 'Image', time: int = 0) -> None:
"""
显示图片并自适应屏幕大小
:param image: CvImage cv2图片
:param title: 窗口标题
:param time: 持续时间
:return:
"""
# 获取图片大小
size = image_size(image)
# 获取自适应大小
adapt_size = suitable_screensize(size)
# float => int
adapt_size = tuple(map(int, adapt_size))
# 改变图片大小
screen_resize = cv2.resize(image, adapt_size)
# 显示图片
show(screen_resize, title, time)
def check_gray(image: CvImage) -> bool:
"""
判断图片是否为灰度图片
灰度图所有位置 r,g,b 全相等
:param image: CvImage cv2图片
:return:
"""
if len(image.shape) < 3:
return True
width, height = image_size(image)
for x in range(width):
for y in range(height):
b, g, r = image[y][x]
if not (b == g == r):
return False
return True