6

I am trying to send a webcam image to browser using Python. Now, I send it using the following code:

def send_a_frame():
 capture = cv2.VideoCapture(0)
 frame = capture.read()[1]
 cv2.imwrite("im1.png",frame)
 cnt = open("im1.png","rb").read()
 b64 = base64.encodestring(cnt)
 html = "<html><img src='data:image/png;base64,"+base64 +"'></html"
 send(html)

How can I save an image and reopen an image and convert to base64 with a single statement?

1
  • could you post the rest of your code please? Commented May 13, 2020 at 21:24

1 Answer 1

16

I had the same problem, in my case I was reading from video file but it should work. Use cv2.imencode() method. See the following code

def send_a_frame():
    capture = cv2.VideoCapture(0)
    frame = capture.read()[1]
    cnt = cv2.imencode('.png',frame)[1]
    b64 = base64.encodestring(cnt)
    html = "<html><img src='data:image/png;base64,"+b64 +"'></html"
    send(html)
1
  • 1
    you can also use cStringIO Commented Jun 6, 2015 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.