如何使用mongodb的Gridfs与PIL(Python图像库)

Moh*_*ati 4 python mongodb python-imaging-library gridfs

我使用mongodb并将文件保存到gridfs

现在我想从gridfs编辑图像...

我用这个代码

def thumbnail(file_obj):
    import StringIO
    from PIL import Image

    im = StringIO.StringIO()

    im.write(file_obj.raw_file)

    im_ful = Image.open(im)

    return im_ful.info
Run Code Online (Sandbox Code Playgroud)

但是pil说"无法识别图像文件"

那个图像也;)如何解决它

Len*_*bro 6

你需要一个im.seek(0)在之前Image.open(im)调用.否则PIL会尝试从文件末尾读取,不会获取任何数据,并且会失败.