将图像转换为二进制流 [英] Convert Image to binary stream
问题描述
我的应用程序有两面,一方面我使用C ++,以便使用Pleora的EBUS SDK从相机中读取相框。当第一次接收到该流时,在将缓冲区转换为图像之前,我可以一次读取流16位,以便为每个像素执行一些计算,即每个像素存在16位数据块
There are two sides to my app, on one side I'm using C++ in order to read the frames from a camera using Pleora's EBUS SDK. When this stream is first received, before I convert the buffer to an image, I am able to read the stream 16 bits at a time in order to perform some calculations for each pixel, i.e. there exists a 16 bit chunk of data for each pixel.
现在下半部分是一个Django网络应用程序,在这里我也提供了这个视频输出,这次通过ffmpeg,nginx,hls流。当用户点击视频时,我想要获取当前帧和点击坐标,并执行与上述C ++部分相同的计算。
Now the second half is a Django web app where I am also presented this video output, this time via an 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 I do above in the C++ portion.
现在我使用html5画布来捕获帧,我使用 canvas.toDataURL()
为了将帧转换为base64编码图像,然后传递base64图像,坐标以及通过AJAX的python的尺寸。
Right now I use an html5 canvas to capture the frame and I use canvas.toDataURL()
in order to convert the frame into a base64 encoded image, I then pass the base64 image, the coordinates, and the dimensions of the frame to python via AJAX.
在python中,我试图操作这个base64编码的图像,以获得每像素16位目前我执行以下操作:
In python I am trying to manipulate this base64 encoded image in such a way as to get 16 bits per pixel. At the moment I do 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'))
然而, b64decoded_frame
中的索引要少得多,那么有像素在图像中,整数值不如预期的那么高。我已经检查,图像是完整的,因为我可以保存为png。
However, there are far fewer indexes in the b64decoded_frame
then there are pixels in the image and the integer values aren't nearly as high as expected. I have checked and the image is intact as I am able to save it as a png.
总而言之,如何将base64映像转换为序列化的二进制流,每个像素由16位表示。
To summarize, how do I convert a base64 image into a serialized binary stream where each pixel is represented by 16 bits.
更新
我忘了提及我正在使用python3.2
I forgot to mention that I'm using python3.2
经过一些更多的研究,我认为,我想要做的是获得mono16值的给定像素。我不知道这是否是我想要做的,但如果有人可以解释如何将图像转换为mono16或像素到mono16,我可以探索,看看它是否实际上是解决方案。 >
And after some more research I think that what I'm trying to do it get the mono16 value of a given pixel. I don't know for sure if that is what I want to do but if anyone could explain how to either convert an image to mono16 or a pixel to mono16 I could explore that and see if it is in fact the solution.
推荐答案
我选择的解决方案是将图像转换为8位灰度图像,然后将所需像素转换为16位,位对应。解决方案如下所示:
The solution that I've chosen is to convert the image into an 8-bit greyscale image then convert the desired pixel into its 16-bit counterpart. The solution looks like the following:
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)
这篇关于将图像转换为二进制流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!