图像编码为bytes
import cv2
# 指定编码为JPEG格式的,要和图片存储的格式一致
img_encode = cv2.imencode('.jpg', img)[1]
data_encode = np.array(img_encode)
str_encode = data_encode.tostring()
with open('img_encode.txt', 'w') as f:
f.write(str_encode)
读取bytes文件为图像
import cv2
with open('img_encode.txt', 'r') as f:
str_encode = f.read()
str_array = np.fromstring(str_encode, np.uint8)
image = cv2.imdecode(str_array, cv2.IMREAD_COLOR)
cv2.imshow("", image)
cv2.waitKey(5000)
来源:CSDN
作者:秋夜魔法师
链接:https://blog.csdn.net/weixin_42464187/article/details/104676961