Skip to content
Advertisement

Turning an Open CV frame into a Base64 encoded JPEG

I want to take a frame from an Open CV webcam stream, produce a JPEG thumbnail and then encode it as Base64 (It will then be sent as an MQTT message, but this is not the problem).

My Python “sendimage” function is:

def sendimage():
    # produce thumbnail image
    thumbnail = imutils.resize(frame, width=320)
    # encode as base64 jpeg
    result, thumbnailjpg = cv2.imencode('.jpg', thumbnail, [cv2.IMWRITE_JPEG_QUALITY, 90])
    encodedimage = "data:image/jpeg;base64,"+base64.b64encode(thumbnailjpg)
    # send via mqtt
    print("sending thubnail image")

It seems to work as far as the cv2.imencode, but the base64.b64encode fails with.

Exception in thread Thread-1: Traceback (most recent call last): File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py”, line 954, in _bootstrap_inner self.run() File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py”, line 892, in run self._target(*self._args, **self._kwargs) File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 3452, in _thread_main self.loop_forever(retry_first_connection=True) File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 1779, in loop_forever rc = self.loop(timeout, max_packets) File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 1181, in loop rc = self.loop_read(max_packets) File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 1572, in loop_read rc = self._packet_read() File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 2310, in _packet_read rc = self._packet_handle() File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 2936, in _packet_handle return self._handle_publish() File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 3216, in _handle_publish self._handle_on_message(message) File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/paho/mqtt/client.py”, line 3444, in _handle_on_message self.on_message(self, self._userdata, message) File “/Users/seanclark/Documents/imagesearch/basic-motion-detection/picam.py”, line 80, in on_message sendimage() File “/Users/seanclark/Documents/imagesearch/basic-motion-detection/picam.py”, line 92, in sendimage encodedimage = “data:image/jpeg;base64,”+base64.b64encode(thumbnailjpg) TypeError: can only concatenate str (not “bytes”) to str

I can see that it is a TypeError, but have tried various things and can’t seem to get rid of it. I wonder if my approach is wrong?

Thanks.

SEan

Advertisement

Answer

I have seen the below used for this same exact reason

import base64
import numpy as np
import cv2

img = cv2.imread('test.jpg')
_, im_arr = cv2.imencode('.jpg', img)  # im_arr: image in Numpy one-dim array format.
im_bytes = im_arr.tobytes()
im_b64 = base64.b64encode(im_bytes)

In the above code, we first save the image in Numpy ndarray format to im_arr which is a one-dim Numpy array. We then get the image in binary format by using the tobytes() method of this array.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement