converting Numpy NDAarray to JPEG in memory

I have an image in the form of an ndarray, I want to get it as a jpeg, but not by saving, but by saving to a variable.

I don't want to save any data to a separate file, I just want to save the jpeg representation to a variable so that I can then use it to request a server that requires this data format, hence the question: how do I do this conversion without creating a separate file? How can I implement this?

Author: MaxU, 2019-02-28

1 answers

In fact, the corresponding ndarray is the image that can be saved to disk in the format you need.

Example:

import io
from imageio import imwrite

with io.BytesIO() as out:
    imwrite(out, img_array, format="JPEG")
    img_contents = out.getvalue()
 1
Author: MaxU, 2019-02-28 15:17:16