mongodb 将图片保存到mongodb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11915770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:47:34  来源:igfitidea点击:

saving picture to mongodb

mongodbpython-imaging-librarytornado

提问by Abdelouahab Pp

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

我正在尝试使用 Tornado 和 pil 以及 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")

and the database code:

和数据库代码:

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!

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

my problem is:

我的问题是:

  • 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.
  • 要了解数据库如何处理图片,我必须制作 image.save(path, format),但是路径,它是正常系统路径(windows 或 linux)的路径吗?
  • 配置文件很简单,我将图片上传限制为500ko,并且mongodb中的文档为16mb,因此该文档将处理整个配置文件,但是对于包含图片的小文档,我是否必须使用gridFS?关键问题出在图片保存的路径上,卡住了,这是我第一次处理数据库,所以很抱歉这个问题。

回答by jdi

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 Userdocument to the avatar picture.

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

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.

除此之外,您还可以将二进制数据直接存储在您的文档中,尽管在您的代码中您没有保存数据。你只是用 来打开它PIL.Image,然后什么都不用做。

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

假设你正在使用pymongo你的驱动程序,我认为你可以做的就是将二进制数据包装在一个Binary容器中,然后存储它。这是我未经测试的,但我认为它应该有效:

from pymongo.binary import Binary

binary_avatar = Binary(avat)

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

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:

如果您要使用 GridFS,它可能如下所示:

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
    ...
}

回答by Shiva

This is the code to insert and retrieve image in mongodb without using gridfs.

这是在不使用 gridfs 的情况下在 mongodb 中插入和检索图像的代码。

def insert_image(request):
    with open(request.GET["image_name"], "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
    print encoded_string
    abc=db.database_name.insert({"image":encoded_string})
    return HttpResponse("inserted")

def retrieve_image(request):
    data = db.database_name.find()
    data1 = json.loads(dumps(data))
    img = data1[0]
    img1 = img['image']
    decode=img1.decode()
    img_tag = '<img alt="sample" src="data:image/png;base64,{0}">'.format(decode)
    return HttpResponse(img_tag)

回答by barzotto

there is an error in :

有一个错误:

          from pymongo.binary import Binary

the correct syntax is:

正确的语法是:

          from bson.binary import Binary

thk you all for your endless support

谢谢大家的无限支持

Luca

卢卡

回答by Thomas Tran

Try using carierwave-mongoid https://github.com/jnicklas/carrierwave-mongoidI think it is a simple and easy way for you in this case

尝试使用 carierwave-mongoid https://github.com/jnicklas/carrierwave-mongoid我认为在这种情况下这对您来说是一种简单易行的方法

回答by Andreas Jung

You need to save binary data using the Binary() datatype of pymongo.

您需要使用 pymongo 的 Binary() 数据类型保存二进制数据。

http://api.mongodb.org/python/2.0/api/bson/binary.html#module-bson.binary

http://api.mongodb.org/python/2.0/api/bson/binary.html#module-bson.binary