将图片保存到mongodb [英] saving picture to mongodb

查看:1157
本文介绍了将图片保存到mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试使用龙卷风,pil和mongodb进行此操作.

am trying yo do this using tornado and pil and mongodb.

avat = self.request.files['avatar'][0]["body"]
nomfich = self.request.files['avatar'][0]["filename"]

try:
    image = Image.open(StringIO.StringIO(buf=avat))
    size = image.size
    type = image.format

    avatar = r"/profile-images/{0}/{1}".format(pseudo, nomfich)

except IOError:
    self.redirect("/erreur-im")

和数据库代码:

user={
    "pseudo": pseudo, 
    "password":password, 
    "email":email, 
    "tel":tel, 
    "commune":commune,    
    "statut":statut, 
    "nom":nom, 
    "prenom":prenom, 
    "daten":daten, 
    "sexe":sexe, 
    "avatar":avatar
}

self.db.essog.insert(user)  

工作正常,头像"被保存,但是没有图像,它只保存了一个名字!

and it worked ok, the "avatar" is saved, but there in no image, it saves only a name!

我的问题是:

  • 要了解数据库如何处理图片,我必须创建image.save(路径,格式),但是该路径是正常系统路径(Windows或Linux)的路径吗?
  • 配置文件很简单,我将图片上传限制为500ko,而mongodb中的文档为16mb,因此该文档将处理整个配置文件,但是当包含图片的小文档时,我是否必须使用gridFS? 关键问题在于图片保存的路径,卡住了,这是我第一次处理数据库,所以对这个问题感到抱歉.
  • to understand how database deals with pictures, must i make image.save(path, format), but the path, is it a path of a normal system path (windows, or linux)?
  • the profile is simple, and i've limited the picture upload to 500ko, and the document in mongodb is 16mb, so the document will handle the entire profile, but must i use gridFS even for small document when it contains picture? the key problem is in path of the picture saving, am stuck, and it's the first time i deal with database, so am sorry for that question.

推荐答案

您不一定需要GridFS在MongoDB中存储文件,但是它肯定会带来更好的体验,因为它可以处理二进制数据的拆分和保存. ,同时使元数据也可用.然后,您可以将User文档中的ID存储到头像图片中.

You don't necessarily need GridFS for storing files in MongoDB, but it surely makes it a nicer experience, because it handles the splitting and saving of the binary data, while making the metadata also available. You can then store an ID in your User document to the avatar picture.

此外,您也可以将二进制数据直接存储在文档中,尽管在代码中您没有保存数据.您只需使用PIL.Image打开它,然后对其不执行任何操作.

That aside, you could also store binary data directly in your documents, though in your code you are not saving the data. You simply are opening it with PIL.Image, but then doing nothing with it.

假设您正在使用pymongo作为驱动程序,我想您所能做的就是将二进制数据包装在

Assuming you are using pymongo for your driver, I think what you can do is just wrap the binary data in a Binary container, and then store it. This is untested by me, but I assume it should work:

from pymongo.binary import Binary

binary_avatar = Binary(avat)

user={
    ...
    "avatar":avatar,
    "avatar_file": binary_avatar
    ...
}

现在这么说...只是让自己变得更轻松,并使用 GridFS .这就是它的意思.

Now that being said... just make it easier on yourself and use GridFS. That is what it is meant for.

如果要使用GridFS,它可能看起来像这样:

If you were to use GridFS, it might look like this:

from gridfs import GridFS

avat_ctype = self.request.files['avatar'][0]["content_type"]

fs = GridFS(db)
avatar_id = fs.put(avat, content_type=avat_ctype, filename=nomfich)

user={
    ...
    "avatar_name":avatar,
    "avatar_id": avatar_id
    ...
}

这篇关于将图片保存到mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆