前言
当使用flask框架的时候,去发送和接收图片,但是官方的是只有一个save方法,只能保存到本地在读取,那我岂不是还得再读出来,这多麻烦
解决
Post发送图片
方式一
info={} with open("1.jpg","rb") as f: files={"file":("1.jpg",f,"image/jpg")} res=requests.post("http://127.0.0.1:10086/photo",data=info,files=files) f.close()
方式二
image=cv2.imread("1.jpg") info={} files={"file":("1.jpg",cv2.imencode(".jpg",image)[1].tobytes(),"image/jpg")}res=requests.post("http://127.0.0.1:10086/photo",data=info,files=files,timeout=3)
Flask接收图片
方式一
buf=request.files["file"].read()
img=cv2.imdecode(np.frombuffer(buf, dtype=np.uint8),flags=cv2.IMREAD_COLOR)
cv2.imshow("a",img)
cv2.waitKey(0)
方式二
from PIL import Image
imgbytes=request.files["file"]
image = Image.open(imgbytes)
image_cv2 = np.array(image)[:,:,::-1]
cv2.imwrite("a.jpg",image_cv2)
方式三
image=request.files["file"]
image.save("test.jpg")