Python实现批量读取图片并存入mongodb数据库的方法示例


Posted in Python onApril 02, 2018

本文实例讲述了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()

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python Django批量导入不重复数据
Mar 25 Python
详解如何在python中读写和存储matlab的数据文件(*.mat)
Feb 24 Python
Python3中的最大整数和最大浮点数实例
Jul 09 Python
详解Python中的各种转义符\n\r\t
Jul 10 Python
Python3中urlencode和urldecode的用法详解
Jul 23 Python
python写入数据到csv或xlsx文件的3种方法
Aug 23 Python
python中bytes和str类型的区别
Oct 21 Python
wxPython+Matplotlib绘制折线图表
Nov 19 Python
Python类和实例的属性机制原理详解
Mar 21 Python
tensorflow指定CPU与GPU运算的方法实现
Apr 21 Python
浅谈Python3多线程之间的执行顺序问题
May 02 Python
Python机器学习三大件之一numpy
May 10 Python
python基础教程项目四之新闻聚合
Apr 02 #Python
Python实现将数据框数据写入mongodb及mysql数据库的方法
Apr 02 #Python
python基础教程项目三之万能的XML
Apr 02 #Python
python opencv检测目标颜色的实例讲解
Apr 02 #Python
浅谈python配置与使用OpenCV踩的一些坑
Apr 02 #Python
python基础教程项目二之画幅好画
Apr 02 #Python
通过Python 接口使用OpenCV的方法
Apr 02 #Python
You might like
使用PHP接收POST数据,解析json数据
2013/06/28 PHP
ThinkPHP单字母函数(快捷方法)使用总结
2014/07/23 PHP
php使用fsockopen函数发送post,get请求获取网页内容的方法
2014/11/15 PHP
使用Composer安装Yii框架的方法
2016/03/15 PHP
thinkPHP中钩子的两种配置调用方法详解
2016/11/11 PHP
PHP符合PSR编程规范的实例分享
2016/12/21 PHP
php实现生成PDF文件的方法示例【基于FPDF类库】
2018/07/21 PHP
ExtJS 2.0实用简明教程 之ExtJS版的Hello
2009/04/29 Javascript
js获取元素在浏览器中的绝对位置
2010/07/24 Javascript
扩展js对象数组的OrderByAsc和OrderByDesc方法实现思路
2013/05/17 Javascript
JQuery之focus函数使用介绍
2013/08/20 Javascript
Javascript 多浏览器兼容总结(实战经验)
2013/10/30 Javascript
jQuery中parents()和parent()的区别分析
2014/10/28 Javascript
JS从一组数据中找到指定的单条数据的方法
2016/06/02 Javascript
jquery表格datatables实例解析 直接加载和延迟加载
2016/08/12 Javascript
jQuery简单创建节点的方法
2016/09/09 Javascript
AngularJS实现用户登录状态判断的方法(Model添加拦截过滤器,路由增加限制)
2016/12/12 Javascript
JS日程管理插件FullCalendar中文说明文档
2017/02/06 Javascript
angularjs $http调用接口的方式详解
2018/08/13 Javascript
JavaScript 自定义html元素鼠标右键菜单功能
2019/12/02 Javascript
[57:16]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS VG第二场
2014/05/26 DOTA
Python中__call__用法实例
2014/08/29 Python
说说如何遍历Python列表的方法示例
2019/02/11 Python
python3.6+django2.0+mysql搭建网站过程详解
2019/07/24 Python
python可视化text()函数使用详解
2020/02/11 Python
python通过对字典的排序,对json字段进行排序的实例
2020/02/27 Python
Python3爬虫关于识别检验滑动验证码的实例
2020/07/30 Python
Python 获取异常(Exception)信息的几种方法
2020/12/29 Python
Java语言的优势
2015/01/10 面试题
法律专业个人实习自我鉴定
2013/09/23 职场文书
党员干部群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
司机岗位职责
2015/02/04 职场文书
关于上班时间调整的通知
2015/04/23 职场文书
学历证明样本
2015/06/16 职场文书
导游词之唐山景点
2019/12/18 职场文书
python-opencv 中值滤波{cv2.medianBlur(src, ksize)}的用法
2021/06/05 Python