working with images in pymongo API
<p>Im doing a pymongo connectivity program that uses python to create a gridfs database on mongodb and adds an image into the database.I've use command line parser to call the methods.I have successfully added the image in the db and it also reflects in the mongodb db but when I call show function it says image not found.
Here is the code :</p>

<pre><code>import argparse
from PIL import Image
from pymongo import Connection
import gridfs
from bson.binary import Binary
# setup mongo
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
# connect to the database &amp; get a gridfs handle
mongo_con = Connection(MONGODB_HOST, MONGODB_PORT)
fs = gridfs.GridFS(mongo_con.ank1)

def add_image():
    """add an image to mongo's gridfs"""
    gridfs_filename = 'ofc2.jpg'
    fileID = fs.put(open('C:\Python27\ofc2.jpg','r'),filename='ofc2.jpg')
    print "created new gridfs file {0} with id {1}".format(gridfs_filename, fileID)

def show():
    """start the flask service"""
    filename='ofc2.jpg'
    if not fs.exists(filename="ofc2.jpg"):
       raise Exception("mongo file does not exist! {0}".format(filename))
    im_stream = fs.get_last_version(filename)
    im = Image.open(im_stream)
    im.show()

def main():
    # CLI
    parser = argparse.ArgumentParser()
    parser.add_argument('--show', action='store_true', help='start the service')
    parser.add_argument('--add', action='store_true', help='add an image via URL')
    args = parser.parse_args()
    if args.show:
        show()
    elif args.add:
        add_image()

main()
</code></pre>

<p>Following is the op:`</p>

<pre><code>D:\&gt;python img.py

D:\&gt;python img.py

D:\&gt;python img.py --add
 created new gridfs file ofc2.jpg with id 580237fb0f84ea10789b20e6

D:\&gt;python img.py --show
 Traceback (most recent call last):
  File "img.py", line 62, in &lt;module&gt;
  main()
  File "img.py", line 57, in main
   show()
  File "img.py", line 47, in show
   im = Image.open(im_stream)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1980, in open
  raise IOError("cannot identify image file")
   IOError: cannot identify image file
</code></pre>

<p>on mongodb</p>

<p>mongodb output</p>

<pre><code>MongoDB Enterprise &gt; show dbs;
ank             0.000GB
ank1            0.000GB
ankit           0.000GB
gridfs_example  0.000GB
local           0.000GB
MongoDB Enterprise &gt; use ank1
switched to db ank1
MongoDB Enterprise &gt; show collections
fs.chunks
fs.files 
 MongoDB Enterprise &gt; db.fs.files.find().pretty()
  {
    "_id" : ObjectId("5802397f0f84ea122c6176a6"),
    "chunkSize" : 261120,
    "filename" : "ofc2.jpg",
    "length" : 335,
    "uploadDate" : ISODate("2016-10-15T14:13:19.882Z"),
    "md5" : "ef937ff6e6a00e64a2dda34251ca03b5"
    }
</code></pre>

<p>i don't know what the problem is ive done the same program in fedora it worked.but in windows im having this problem is that related to os or the python versions</p>

<p>You may need to convert the <code>im_stream</code> to bytes for reading using the <a href="https://docs.python.org/2/library/io.html#buffered-streams" rel="nofollow">BytesIO</a> class.</p>

<pre><code>im = Image.open(io.BytesIO(im_stream.read()), 'r')
</code></pre>

python mongodb pymongo