Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

python imaging library - saving picture to 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")

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...