python openCV 图片编码保存至本地与从本地读取编码文件(编码和解码)

import cv2
import numpy as np

###====保存图片======###
img = cv2.imread(r"C:/Users/abc/Desktop/qq/animal.jpg")
# '.jpg'表示把当前图片img按照jpg格式编码,按照不同格式编码的结果不一样
img_encode = cv2.imencode('.jpg', img)[1]
# imgg = cv2.imencode('.png', img)
data_encode = np.array(img_encode)
str_encode = data_encode.tostring()

# 缓存数据保存到本地
with open('demoImage.txt', 'wb') as f:
    f.write(str_encode)
    f.flush()

###====读取图片======###

with open('demoImage.txt', 'rb') as f:
    str_encode = f.read()
nparr = np.fromstring(str_encode, np.uint8)
img_decode = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow("Original", img_decode)
cv2.waitKey()

 

你可能感兴趣的:(pthon-opencv,图像处理)