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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With