我试图从BytesIO中保存一个Bytes对象中的多张图片。稍后,从字节到图片再次访问这些图片。但我当然对字节不太了解。是否有一种方法可以在一张图片之后放入缓冲区,然后它知道图片的结尾位置?客户端:
#cv2 Capture Frame
ret, frame = cls.__cap.read()
buf = BytesIO()
for i in range(10):
Image.fromarray(frame).save(buf, format='JPEG')
# Here the Logic for differentiating Pictures
body = buf.getvalue()
requests.post(url,data=body,headers={'content-type': 'application/x-image'})网站侧:
images_as_bytes = BytesIO(flask.request.data)
for i in range(10):
# Here the Logic for differentiating Pictures
img = Image.open(images_as_bytes)
frame = np.asarray(img)
# process发布于 2022-02-04 09:55:39
我不确定您所做的工作的合理性或有效性,但您也许可以利用以下事实:所有JPEG都以EOI标记结尾:
0xff 0xd9它们以SOI标记开始,即:
0xff 0xd8同样,PNG图像的开头是:
0x89 0x50 0x4e 0x47 0x0d 0x0a 0x1a 0x0a并以以下方式结束:
0x49 0x45 0x4e 0x44 0xae 0x42 0x60 0x82下面是一些完全未经测试的、相关的代码,可以让您了解如何分割2个级联的PNG图像:
#!/usr/bin/env python3
import mmap
import re
pattern = re.compile(rb'(\x89PNG\x0d\x0a\x1a\x0a.*?\x00\x00\x00\x00IEND\xAE\x42\x60\x82)', re.MULTILINE|re.DOTALL)
# I guess we could do 2 TIFFs just as easily, or JPEGs
with open('TwoConcatenatedPNGs.png', 'rb') as f:
with mmap.mmap(f.fileno(), 0,
access=mmap.ACCESS_READ) as m:
i = pattern.finditer(m)
for p in i:
print(p.span())创建这样的测试文件:
cat IMAGE1.PNG IMAGE2.PNG > TwoConcatenatedPNGs.pnghttps://stackoverflow.com/questions/70984157
复制相似问题