您不需要将缓冲区保存到文件。以下脚本从网络摄像头捕获图像,将其编码为JPG图像,然后将该数据转换为可打印的base64编码,该编码可与JSON一起使用:
import cv2import base64cap = cv2.VideoCapture(0)retval, image = cap.read()retval, buffer = cv2.imenpre('.jpg', image)jpg_as_text = base64.b64enpre(buffer)print(jpg_as_text)cap.release()
给你一些开始像:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCg
可以扩展它以显示如何将其转换回二进制,然后将数据写入测试文件以显示转换成功:
import cv2import base64cap = cv2.VideoCapture(0)retval, image = cap.read()cap.release()# Convert captured image to JPGretval, buffer = cv2.imenpre('.jpg', image)# Convert to base64 encoding and show start of datajpg_as_text = base64.b64enpre(buffer)print(jpg_as_text[:80])# Convert back to binaryjpg_original = base64.b64depre(jpg_as_text)# Write to a file to show conversion workedwith open('test.jpg', 'wb') as f_output: f_output.write(jpg_original)
要将图像作为图像缓冲区(而不是JPG格式)取回,请尝试:
jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)image_buffer = cv2.imdepre(jpg_as_np, flags=1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)