Skip to content Skip to sidebar Skip to footer

Retrieve Stored Image From Mongodb Using Python

from pymongo import MongoClient from bson.objectid import ObjectId import numpy as np import gridfs import os,os.path i=0 try: for file in os.listdir('/Users/sarthakgupta/Des

Solution 1:

Create your fs variable like before, and:

data = fs.get_last_version(filename).read()

You could also query for a list of files like:

from bson import Regex
for f in fs.find({'filename': Regex(r'.*\.(png|jpg)')):
    data = f.read()

Also, a comment about your code: it's very slow to recreate the MongoClient and GridFS instances for every iteration of your loop. Create them once before you start looping, and reuse them.

Post a Comment for "Retrieve Stored Image From Mongodb Using Python"