简体   繁体   中英

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.

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.

What is the most efficient way to send these? What would the code look like in 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.

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
  2. Add custom HTTP headers with your fields in
  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? I expect it's far less than the latency of an HTTP request. 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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