可以通过cv2直接加载上传的图像吗?

ero*_*gol 7 python opencv flask

我在内存中有一个上传的文件.我想用cv2操纵文件.目前,我将文件写入磁盘,然后使用cv2读取它.如何跳过编写文件并直接用cv2加载?

file = request.files['file']
# if file and allowed_file(file.filename):

# save file            
filename = secure_filename(file.filename)
file_path = os.path.join(dressrank.config['SHOW_IMG_FOLDER'], filename);
file.save(file_path)
img_path = file_path

# COLOR FATURE EXTRACTION
img = read_img(img_path)
img =img_resize(img, 500)
Run Code Online (Sandbox Code Playgroud)

dav*_*ism 18

使用上传的数据构建一个numpy数组.使用cv2解码此数组.

img = cv2.imdecode(numpy.fromstring(request.files['file'].read(), numpy.uint8), cv2.IMREAD_UNCHANGED)
Run Code Online (Sandbox Code Playgroud)

在OpenCV 3.0之前,请cv2.CV_LOAD_IMAGE_UNCHANGED改用.


另请参见:Python OpenCV从字节字符串加载图像