Python bulk reading of pictures and database implementation

Source: Internet
Author: User
This time for everyone to bring Python bulk read the picture and the implementation of the database, Python bulk read the picture and deposit into the database of the attention to what, the following is the actual case, to see together.

This article explains how Python implements bulk-read images and stores them in a MongoDB database. Share to everyone for your reference, as follows:

My picture is placed in E:\image\, and then I use Python to read the picture and then display one, save a picture (which can be commented out), and deposit the image by Gridfs. The code is as follows:

#--* Coding=utf-8 *--from cstringio import stringiofrom pymongo import mongoclientimport gridfsimport osimport matplotlib . Pyplot as Pltimport matplotlib.image as Imingimport bson.binaryimport numpy as npif name = = ' main ':  connect = MONGOCL Ient (' 127.0.0.1 ', 27017) # Create connection point  db = Connect.mydb  print db.collection_names ()  imgput = Gridfs. GRIDFS (db)  dirs = ' G:\image '  files = Os.listdir (dirs) for  file in files:    filesname = dirs + ' \ \ ' + File
  print filesname    imgfile=iming.imread (filesname)    # iming.imsave (' s.jpg ', imgfile)    # Print type ( Imgfile), Imgfile    # imgfile.shape ()    plt.imshow (imgfile)    plt.axis (' off ') plt.show    ()    f= File.split ('. ')    Print F    datatmp=open (Filesname, ' RB ')    Data=stringio (Datatmp.read ())    content=bson.binary.binary ( Data.getvalue ())    # Print content    insertimg=imgput.put (data,content_type=f[1],filename=f[0])    Datatmp.close ()

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Python Virtual Tea Party effect

Python3.5 How to install OpenCV in WINDOW10

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.