python传递numpy数据的几种方法【request.post传递参数】

1.以文件的形式发送

requests.post(api_url, files={'image': open(img_path, 'rb')})

2.以编码数组的形式发送

# image_np is a numpy array
_, img_encoded = cv2.imencode('.jpg', image_np)
requests.post(api_url, data=img_encoded.tobytes())

3.作为缓冲区发送

buf = io.BytesIO()
plt.imsave(buf, image_np, format='jpg')
image_data = buf.getvalue()
requests.post(api_url, data=image_data)

4.以字符串base64发送:

with open(img_path, 'rb') as fp:
    img_encoded = str(b64encode(fp.read()))
    
r = requests.post(api_url, json={'image': img_encoded})

你可能感兴趣的:(python)