Implementation of python batch reading of images and storing them in the database

php中世界最好的语言
Release: 2018-04-09 13:43:45
Original
5089 people have browsed it

这次给大家带来python批量读取图片且存入数据库的实现,python批量读取图片且存入数据库的注意事项有哪些,下面就是实战案例,一起来看一下。

本文实例讲述了Python实现批量读取图片并存入mongodb数据库的方法。分享给大家供大家参考,具体如下:

我的图片放在E:\image\中,然后使用python将图片读取然后,显示一张,存入取一张(可以注释掉显示图片的语句),通过Gridfs的方式存入图片。代码如下:

# --* coding=utf-8 *--
from cStringIO import StringIO
from pymongo import MongoClient
import gridfs
import os
import matplotlib.pyplot as plt
import matplotlib.image as iming
import bson.binary
import numpy as np
if name == 'main':
  connect = MongoClient('127.0.0.1', 27017) # 创建连接点
  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()
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

python虚拟茶话会效果

在Window10中Python3.5怎么安装opencv

The above is the detailed content of Implementation of python batch reading of images and storing them in the database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!