-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
51 lines (44 loc) · 1.47 KB
/
utils.py
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
import cv2
import numpy as np
def bytes_to_numpy(image_bytes, channels='RGB'):
"""
图片格式转换 bytes -> numpy
args:
image_bytes(str): 图片的字节流
channels(str): 图片的格式 ['BGR'|'RGB']
return(array):
转换后的图片
"""
_image_np = np.frombuffer(image_bytes, dtype=np.uint8)
image_np = cv2.imdecode(_image_np, cv2.IMREAD_COLOR)
if channels == 'BGR':
return image_np
elif channels == 'RGB':
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
return image_np
def numpy_to_bytes(image_np, channels='RGB'):
"""
图片格式转换 numpy -> bytes
args:
image_np(array): numpy格式的图片数据
channels(str): 图片的格式 ['BGR'|'RGB']
return(str):
图片的字节流
"""
if channels == 'RGB':
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
image_bytes = np.array(cv2.imencode('.jpg', image_np)[1]).tobytes()
return image_bytes
def numpy_to_pic(image_path, image_np, channels='BGR'):
"""
保存图片
args:
image_path(str): 图片路径
image_np(array): numpy格式的图片数据
channels(str): 图片的格式 ['BGR'|'RGB']
"""
if channels == 'BGR':
cv2.imwrite(image_path, image_np)
elif channels == 'RGB':
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
cv2.imwrite(image_path, image_np)