如何使用python-restapi序列化blob数据?

2024-03-29 04:45:32 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我创建了如下表:

类ImageData(db.Model):

id = db.Column(db.Integer, primary_key = True)
uname = db.Column(db.VARCHAR(20), nullable = True, unique=True)
fname = db.Column(db.VARCHAR(20), nullable = True)
lname = db.Column(db.VARCHAR(20), nullable = True)
email = db.Column(db.VARCHAR(50), nullable = True, unique=True)
image = db.Column(db.LargeBinary, nullable=True)

图像列将以blob格式存储图像数据

这样我就节省了:

如果request.file中有“image”:

        imagedata = request.files['image']
        imageD = imagedata.read()
    image = ImageData(
        uname = request.form['uname'],
        fname = request.form['fname'],
        lname = request.form['lname'],
        email = request.form['email'],
        image = imageD
    )
    db.session.add(image)
    db.session.commit()

我的要求是序列化图像数据,这样我就可以从API中获取图像数据

但其显示错误如下所示: UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0xff:无效的开始字节

回溯(最近一次呼叫最后一次) 文件“C:\Users\Python\ImageProcessApi\venv\Lib\site packages\flask\app.py”,第2464行,在调用中

def __call__(self, environ, start_response):
    """The WSGI server calls the Flask application object as the
    WSGI application. This calls :meth:`wsgi_app` which can be
    wrapped to applying middleware."""
    return self.wsgi_app(environ, start_response)

def __repr__(self):
    return "<%s %r>" % (self.__class__.__name__, self.name)

Tags: 数据图像imageselfformtruedbemail