Python OpenCV convert image to byte string?

前端 未结 6 1800
礼貌的吻别
礼貌的吻别 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 12:03

    It works in 2020 with numpy==1.19.4 and opencv==4.4.0:

    import cv2
    
    cam = cv2.VideoCapture(0)
    
    # get image from web camera
    ret, frame = cam.read()
    
    # convert to jpeg and save in variable
    image_bytes = cv2.imencode('.jpg', frame)[1].tobytes()
    

提交回复
热议问题