1
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/Desktop/sae/Images"):
        if (file.endswith(".png") | file.endswith(".jpg")):
            filename = "/Users/sarthakgupta/Desktop/sae/Images/"+file
            datafile =  open(filename,"rb")
            thedata = datafile.read()
            datafile.close()


            c = MongoClient()
            i=i+1
            db = c.trial5
            fs = gridfs.GridFS(db)
            t = "class"+str(i)


            stored = fs.put(thedata,filename=q)

except IOError:
    print("Image file %s not found" %datafile)
    raise SystemExit

I stored image in a mongo db . Now i want to retrieve those images from database by the filename and to store the image(or pixels) of same filename in an array or list. Suppose if there is 2 images with filename "class1",then they should be in one array.

3
  • Hope my answer in another question may help you. stackoverflow.com/questions/22077720/… Commented Mar 22, 2017 at 11:28
  • What is "q" here in filename=q? Commented Oct 7, 2017 at 9:15
  • I found q should be replaced by t instead. Commented Oct 7, 2017 at 9:50

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

When i use : 'data = fs.get_last_version(filename='class3') print(data)' i get the output : <gridfs.grid_file.GridOut object at 0x105279e80> . What does this mean ? and when i use 'data = fs.get_last_version(filename='class3').read() print(data)'. I get the output "b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00H\x00H\x00\x00\xff\xe1\x00XExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x02\x0......" in this form. How can i use this output ?.. Thanks for the suggestion
"data" is the byte contents of the image. You could save those contents to a file named "image.jpg" or do whatever you want with them.
But when i tried to display the image using open cv , i get an error " a bytes-like object is required, not 'GridOut' " . Here's my code. nparr = np.fromstring(data1, np.uint8) img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) print(img_np)
f is a GridOut object. f.read() is the contents of the image. Pass f.read() to OpenCV.
the for loop method is still showing error illegal expression on Regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.