def image_to_file(image): f = StringIO() ret, buf = cv2.imencode('.jpg', image) f.write(np.array(buf).tostring()) f.seek(0) return f