python批量读取图片且存入数据库的实现


本文摘自php中文网,作者php中世界最好的语言,侵删。

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

# --* 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()

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

推荐阅读:

python虚拟茶话会效果

在Window10中Python3.5怎么安装opencv

以上就是python批量读取图片且存入数据库的实现的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

详解Python中startswith()函数与endswith函数的使用方法

Python中如何合并两个字典的示例分享

Python使用代理ip访问网站

Python中如何使用字符串调用函数与方法的示例分享

Python怎么新建项目

关于Python中的中文编码问题

Python卸载容易吗

Python实现获取前100组勾股数的方法

Python中回调的含义详解

深入了解Python中的协程函数

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...