title: Save jpg image to mongodb using gridfs with python and retrieve it from database when needed
body: <p>I'm trying to save jpg image to mongodb using gridfs with python and retrieve it from database when needed. It looks like it saves fine, then I store its <code>fileid</code> in collection and when reading I use it to locate the image. But it deosn't give me back the image I saved before.</p>
  
  <p>Here is the class definition:</p>
  
  <pre><code>class dbase():
      database=""
  
  def __init__(self,dbserver,dbport ):
      from pymongo import MongoClient
  
      client =MongoClient()
      self.database = client['test']
  
  def Savetodb(self,event):
      import gridfs
      import tkMessageBox
      result = tkMessageBox.askokcancel("Save to db???", "Confirm Save")
      if result:
      # get image name and image file from the widget box
          filename=event.widget.cget("text")
          img =event.widget.cget("image")
          fs = gridfs.GridFS(self.database)
          fileid = fs.put(img,filename=filename)
          self.database.my_collection.insert_one({"imagefile":filename,"fileid":fileid})
  
  
  def Getfromdb(self,outfilename):
      import gridfs
      fs = gridfs.GridFS(self.database)
      fid = ""
  
      for item in self.database.my_collection.find({"imagefile":outfilename}):
          fid=item["fileid"]
      if fid &lt;&gt; "":
          outputdata = fs.get(fid).read()
          return outputdata
  </code></pre>