加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:09:48 所属栏目:Python 来源:网络整理
导读:本篇章节讲解Python实现批量读取图片并存入mongodb数据库的方法。供大家参考研究具体如下: 我的图片放在E:image中,然后使用python将图片读取然后,显示一张,存入取一张(可以注释掉显示图片的语句),通过Gridfs的方式存入图片。代码如下: #

本篇章节讲解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数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

您可能感兴趣的文章:

  • Python 读取图片文件为矩阵和保存矩阵为图片的方法
  • Python读取图片为16进制表示简单代码
  • python使用opencv读取图片的实例
  • Python读取图片属性信息的实现方法
  • python从网络读取图片并直接进行处理的方法
  • Python读取图片EXIF信息类库介绍和使用实例
  • python读取图片并修改格式与大小的方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读