Search code examples
numpyopencvpng

Is there a way to convert numpy array to PNG/JPG... payload without saving it as a file?


Suppose there exists a numpy array, data. I am trying to do the equivalent of the following

cv2.imwrite(filename, data)
with open(filename, 'rb') as fp:
     data_compressed = filename.read()

without having to write to a file. Is there a way to convert numpy array to its equivalent PNG/JPG... representation without having to write to a file and read it as binary?


Solution

  • As Miki pointed out, imencode(...) is the solution.