OpenCV与二进制文件互相转换

墨蓝 2023-10-02 10:06 23阅读 0赞

一、硬盘读取二进制文件

  1. # 二进制转numpy
  2. with open(image_path, "rb") as file:
  3. jpg_bin = file.read()
  4. image = cv2.imdecode(np.asarray(bytearray(jpg_bin),dtype='uint8'), cv2.IMREAD_COLOR)
  5. # numpy转二进制
  6. with open(tmp_image_path, 'wb') as tmp_file:
  7. tmp_jpg_bin = np.array(cv2.imencode('.jpg', image)[1]).tobytes()
  8. tmp_file.write(tmp_jpg_bin)

二、Flask上传直接转换numpy

  1. file = request.files.get('file')
  2. image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)

发表评论

表情:
评论列表 (有 0 条评论,23人围观)

还没有评论,来说两句吧...

相关阅读