简体   繁体   English

如何通过 HTTP 发送编码和解码图像

[英]How to send encoded and decoded image over HTTP

I need to send an encoded and decoded image along with some metadata via HTTP. I would like to send the images as binary data instead of encoding them as base64 as encoding & decoding adds unnecessary latency.我需要通过 HTTP 发送编码和解码图像以及一些元数据。我想将图像作为二进制数据发送,而不是将它们编码为 base64,因为编码和解码会增加不必要的延迟。

So for example, the encoded image may look like this:因此,例如,编码后的图像可能如下所示:

img = open(img_file, 'rb').read()

and the decoded image may look like this:解码后的图像可能如下所示:

img = cv2.imread(img_file)

Assume I also need to send some additional information in POST request, such as the image name for example.假设我还需要在 POST 请求中发送一些附加信息,例如图像名称。

What is the most efficient way to send these?发送这些最有效的方法是什么? What would the code look like in Python? Python 中的代码是什么样的? What content-type or other headers would I need to use?我需要使用什么内容类型或其他标头?

I've found some examples like this online, but they only send a single image and therefore set the content-type as image/jpeg , but I'm wondering what happens when you have additional fields to send.我在网上找到了一些这样的例子,但它们只发送一个图像,因此将内容类型设置为image/jpeg ,但我想知道当你有额外的字段要发送时会发生什么。

If you want to send additional fields you have a few options:如果你想发送额外的字段,你有几个选择:

  1. Base64 encode the image data and embed it in a json string with all your extra data Base64 对图像数据进行编码并将其与所有额外数据一起嵌入到 json 字符串
  2. Add custom HTTP headers with your fields in使用您的字段添加自定义 HTTP 标头
  3. Add your fields to the image metadata itself将您的字段添加到图像元数据本身

I know you said you didn't want to do 1, but how do you know it is adding unnecessary latency if you've never tried it?我知道你说过你不想做 1,但如果你从未尝试过,你怎么知道它会增加不必要的延迟? I expect it's far less than the latency of an HTTP request.我希望它远小于 HTTP 请求的延迟。 Option 2 is risky as the headers can get stripped or changed by.network infrastructure and your users might not expect to find data in the headers.选项 2 存在风险,因为标头可能会被网络基础结构剥离或更改,并且您的用户可能不希望在标头中找到数据。 Option 3 depends a bit what the data is and whether it makes sense for it to be inside the image (and again whether your users know to look for it there)选项 3 在一定程度上取决于数据是什么以及将其放在图像内部是否有意义(以及您的用户是否知道在那里寻找它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM