Convert image to binary stream

There are two sides to my application, on the one hand I use C ++ to read frames from the camera using the Pleora EBUS SDK. When this stream is first received, before I convert the buffer to an image, I can read the stream 16 bits at a time to do some calculations for each pixel, i.e. There is a 16-bit piece of data for each pixel.

Now the second half is the Django web application, where I also presented this video output, this time through the ffmpeg, nginx, hls stream. When the user clicks on the video, I want to be able to take the current frame and the coordinates of their click and perform the same calculation as me, above in the C ++ part.

Right now I am using html5 canvas to capture the frame, and I am using canvas.toDataURL()to convert the frame to a base64 encoded image, then transfer the base64 image, frame coordinates and dimensions to python via AJAX.

In python, I am trying to manipulate this base64 encoded way in such a way as to get 16 bits per pixel. At the moment, I am doing the following:

pos = json.loads(request.GET['pos'])
str_frame = json.loads(request.GET['frame'])
dimensions = json.loads(request.GET['dimensions'])

pixel_index = (dimensions['width'] * pos['y']) + pos['x'] + 1

b64decoded_frame = base64.b64decode(str_frame.encode('utf-8'))

However, the b64decoded_framenumber of indices is much smaller, then there are pixels in the image, and the integer values ​​are not as high as expected. I checked and the image is not damaged as I can save it as png.

To summarize, how to convert a base64 image to a serialized binary stream, where each pixel is represented by 16 bits.

UPDATE

I forgot to mention that I am using python3.2

, , , mono16 . , , , - , mono16 mono16, , .

0
1

, , , 8- , 16- . :

import base64
import io
from PIL import Image

if request.method == 'GET':
    if request.GET['pos'] and request.GET['frame']:
        pos = json.loads(request.GET['pos'])
        str_frame = json.loads(request.GET['frame'])

        # Converts the base64 string into a byte string, we need to encode
        # str_frame as utf-8 first otherwise python3.2 complains about unicode
        b64decoded_frame = base64.b64decode(str_frame.encode('utf-8'))

        # This puts the decoded image into a buffer so that I don't need to save
        # it to disk to use it in PIL
        byte_stream = io.BytesIO(b64decoded_frame)

        # Open the image and convert it to 8-bit greyscale (mono8)
        img = Image.open(byte_stream).convert('L')

        # Get the 8-bit pixel value
        pixel_val = img.getpixel((pos['x'], pos['y']))

        # Convert the 8-bit pixel value to 16-bit by holding the rations
        # i.e. n8 / (2^8 - 1) == x16 / (2^16 - 1)
        pixel_val = int(pixel_val / 255 * 65535)
0

Source: https://habr.com/ru/post/1569010/


All Articles