python 中使用 opencv 上传图片
不保存减少磁盘使用
cv2是存在有
cv2.imencode()
和
cv2.imdecode()
拍照得到图片后
或者读取图片后
可以不用常规的
files = {'file': open("xx.jpg","rb")}
可用
// 其中img为你获取到的图片
// 其类型应该和用 cv2.imread() 读取到的一样
// 通常用opencv调用相机拍照得到的应该就是这种数据类型
// type(img) <class 'numpy.ndarray'>
//
img_encode = cv2.imencode('.jpg', img)[1]
data_encode = np.array(img_encode)
str_encode = data_encode.tobytes()
files = {'file': str_encode}
data = dict(a=a,b=b)
ret = requests.post("http://xxx.xxx.xxx.xxx/xxx/",data = data,files = files)
这种方式对于摄像头拍摄到的图片可以直接操作
减少一次存盘读取
速度上暂时没有足够的量进行测试
接收和正常文件接收一样可以直接接收,网上代码很多
或者
imgfile = request.FILES.get('file')
imgfile = np.asarray(bytearray(imgfile.read()), dtype="uint8")
nparr = cv2.imdecode(imgfile, cv2.IMREAD_ANYCOLOR)
cv2.imwrite(path,nparr)
最后提醒一句,别忘了最开头的
import cv2