Python 如何在PIL opencv web和mongoDB之间转换图像格式?

Python 如何在PIL opencv web和mongoDB之间转换图像格式?,python,image,mongodb,opencv,pillow,Python,Image,Mongodb,Opencv,Pillow,我想将图像从web存储到MongoDB,但首先我将通过opencv检查图像,以确保它是一个血液检测报告图像,就像下面的代码片段一样: if 'imagefile' not in request.files: abort(400) imgfile = request.files['imagefile'] if imgfile.filename == '': abort(400) if imgfile: #pil = Strin

我想将图像从web存储到MongoDB,但首先我将通过opencv检查图像,以确保它是一个血液检测报告图像,就像下面的代码片段一样:

if 'imagefile' not in request.files:
        abort(400)
    imgfile = request.files['imagefile']
    if imgfile.filename == '':
        abort(400)
    if imgfile:
        #pil = StringIO(imgfile)
        #pil = Image.open(pil)
        img = cv2.imdecode(numpy.fromstring(imgfile.read(), numpy.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
        filtered = ImageFilter(image=img).filter()
        if filtered is None:
            return jsonify({"error": "please make sure your picture is perfect"})
        # save to mongo
        content = StringIO()
        filtered.save(content, format="JPEG")
        fid, filename= save_file(content,imgfile.name)
ImageFilter接受opencv格式的图像,并执行过滤等操作,然后返回一个PIL图像,它成功了!然后我将PIL图像保存到MongoDB,代码如下:

def save_file(content, name):

    # content = StringIO(f.read())
    try:
        mime = Image.open(content).format.lower()
        if mime not in app.config['ALLOWED_EXTENSIONS']:
            raise IOError()
    except IOError:
        abort(400)
    c = dict(content=bson.binary.Binary(content.getvalue()),
             filename=secure_filename(name), mime=mime)
    db.files.save(c)
    return c['_id'], c['filename']
它成功了!然后我有另一个功能,从MongoDB中通过id查找图像,然后我将使用它进行OCR

def get_report(fid):
    try:
        file = db.files.find_one(bson.objectid.ObjectId(fid))
        if file is None:
            raise bson.errors.InvalidId()
        print(type(file['content']))

        img = cv2.imdecode(numpy.fromstring(dumps(file['content']), numpy.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
        if img is None:
            return jsonify({"error": "please make sure your picture is perfect"})
        report_data = ImageFilter(image=img).ocr(22)
        print report_data
        if report_data is None:
            return jsonify({"error": "can't ocr'"})
        return jsonify(report_data)
    except bson.errors.InvalidId:
        flask.abort(404)
同样,我将以opencv格式使用它,因此我将把bson.binary.binary转换为opencv图像,但它失败了!因为img总是一个也没有

img = cv2.imdecode(numpy.fromstring(dumps(file['content']), numpy.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
所以,我的最后一个问题是python中真正的图像格式是什么,我如何在web mongodb opencv pil和内存中转换它!,下面是我尝试过的一种方法,但失败了!我想先使用image.frombytes将二进制图像转换为PIL图像,然后将PIL转换为opencv。但是错误:value错误:图像数据不足

# -*- coding: utf-8 -*-
import os
from pymongo import MongoClient
import bson
from PIL import Image
from imageFilter import ImageFilter
import cv2
import numpy
from bson.json_util import dumps

db = MongoClient('localhost', 27017).test
 file =db.files.find_one(bson.objectid.ObjectId("58454666a235ec451d3bf2e6"))
if file is None:
    raise bson.errors.InvalidId()

print(type(file['content']))

# this is success, I use Flask Response
#return Response(file['content'], mimetype='image/' + file['mime'])

# file['content']是整个图片文件的二进制对象,也就是说是一个文件,不应该直接作为二进制数据传递给Image
Image.frombytes(mode='RGB',size=(1000,760),data=file['content'])

img = cv2.imdecode(numpy.fromstring(dumps(file['content']), numpy.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
if img is None:
    print "img is None"
# ImageFilter accept opencv img to process it by opencv
report_data = ImageFilter(image=img).ocr(22)
print report_data

为什么不将映像保存到磁盘并将路径存储在MongoDB中?为什么不将映像保存到磁盘并将路径存储在MongoDB中?