如何将MongoDB中的图像加载到PIL对象中?

问题描述 投票:0回答:1

我使用GridFS将一些图像存储在MongoDB数据库中。我想直接将它们加载到PIL对象中,而不保存在硬盘驱动器上。

我的尝试是:

    f_id = mydb.images.files.find_one({ "filename" : "moto.jpeg" },{ "_id" : 1 })
    data = fs.get(f_id['_id']).read()

    img = Image.open(BytesIO(data))

但出现错误:OSError:无法识别图像文件<_io.bytesio>

我也尝试了BytesIO_object.seek(0),但无济于事。我该怎么办?

python mongodb python-imaging-library gridfs
1个回答
0
投票

Image.open takes a filepath as parameter,而不是图像的内容。

将文件路径传递给Image.open,它应该可以工作

© www.soinside.com 2019 - 2024. All rights reserved.