OpenCV与二进制文件互相转换
一、硬盘读取二进制文件
# 二进制转numpy
with open(image_path, "rb") as file:
jpg_bin = file.read()
image = cv2.imdecode(np.asarray(bytearray(jpg_bin),dtype='uint8'), cv2.IMREAD_COLOR)
# numpy转二进制
with open(tmp_image_path, 'wb') as tmp_file:
tmp_jpg_bin = np.array(cv2.imencode('.jpg', image)[1]).tobytes()
tmp_file.write(tmp_jpg_bin)
二、Flask上传直接转换numpy
file = request.files.get('file')
image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
还没有评论,来说两句吧...