{"nbformat": 4, "cells": [{"source": ["import sys\n", "import pymongo\n", "from pprint import pprint as pp\n", "from bson.objectid import ObjectId\n", "from PIL import Image\n", "import io"], "outputs": [], "metadata": {"_cell_guid": "2feebc6b-4de9-4880-a7cc-a69a19687301", "collapsed": true, "_uuid": "a7c894061945e3cbd8a07425431d90773d8bed14"}, "execution_count": null, "cell_type": "code"}, {"source": ["# restore the train.bson file into a mongoDB database named 'cdiscount' and collection named 'images'\n", "# using default/standard ports for mongoDB, use pymongo to retrieve images selectively.\n", "from pymongo import MongoClient\n", "import datetime\n", "#client = MongoClient()\n", "#client = MongoClient('mongodb://localhost:27017/')\n", "client = MongoClient('localhost', 27017)\n", "#db = client.cdiscount\n", "db = client['cdiscount']\n", "#collection = db.images\n", "imagescollection = db['images']\n", "imagescollection.count()\n", "print(\"collections count -->\" + str(imagescollection.count()))\n", "db.collection_names(include_system_collections=False)\n", "cursor = imagescollection.find({\"category_id\":1000010683})\n", "print(\"Cursor count -->\" + str(cursor.count()))\n", "for rec in cursor:\n", "    #print(rec['imgs'])\n", "    for item in rec['imgs']:\n", "        #print(item['picture'])\n", "        f = io.BytesIO(item['picture'])\n", "        img = Image.open(f)\n", "        print (img.size)\n", "        img.show()\n", "\n", "        client.close()"], "outputs": [], "metadata": {"_cell_guid": "40e4e960-4392-43a0-9a26-8db0de7cf24b", "collapsed": true, "_uuid": "fd67bcb6a26b3a99ad213dc5deb9ff68a28ddf2d"}, "execution_count": null, "cell_type": "code"}], "nbformat_minor": 1, "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "version": "3.6.1", "name": "python", "mimetype": "text/x-python", "nbconvert_exporter": "python"}}}