Python OpenCV convert image to byte string?

前端 未结 6 1789
礼貌的吻别
礼貌的吻别 2020-12-02 11:12

I\'m working with PyOpenCV. How to convert cv2 image (numpy) to binary string for writing to MySQL db without a temporary file and imwrite?

I googled it

6条回答
  •  执笔经年
    2020-12-02 11:50

    My code to use opencv with python cgi :

        im_data = form['image'].file.read()
        im = cv2.imdecode( np.asarray(bytearray(im_data), dtype=np.uint8), 1 )
        ret, im_thresh = cv2.threshold( im, 128, 255, cv2.THRESH_BINARY )
        self.send_response(200)
        self.send_header("Content-type", "image/jpg")
        self.end_headers()      
        ret, buf = cv2.imencode( '.jpg', im_thresh )
        self.wfile.write( np.array(buf).tostring() )
    

提交回复
热议问题