最近做一些数据库调研的工作,目标是实现影像更快的入库、出库、查询,并实现并行访问等操作。

将结果总结成一个mongoImg类,也算是小结吧。

 '''
Created on 2013-8-6
class mongoInsert
@author: tree
'''
__metaclass__ = type import os
from pymongo.database import Database
import time
import gridfs class mongoImg(object):
"""mongoInsert is a class for inserting document """
def __init__(self, database, dir):
"""Create a new instance of :class:mongoInsert
:Parameters:
- `database`: database to use
- `dir` : directory of document
"""
if not isinstance(database, Database):
raise TypeError("database must be an instance of Database")
if len(dir) < 1:
raise TypeError("dir must be an string of directory") # self.__con = Connection()
self.__imgdb = database
self.__imgfs = gridfs.GridFS (self.__imgdb)
self.__dir = dir
self.__filelist=[] #save filepath in list.txt
def __dirwalk(self,topdown=True):
"""traverse the documents of self.__dir and save in self.__filelist
"""
sum=0
self.__filelist.clear() for root,dirs,files in os.walk(self.__dir,topdown):
for name in files:
sum+=1
temp=os.path.join(root,name)
self.__filelist.append(temp)
print(sum) #insert image
def insert(self):
"""insert images in mongodb
"""
self.__dirwalk() tStart = time.time()
for fi in self.__filelist:
with open (fi,'rb') as myimage:
data=myimage.read()
self.__imgfs.put(data, content_type = "jpg", filename =fi) tEnd =time.time ()
print ("It cost %f sec" % (tEnd - tStart)) #get image by filename
def getbyname(self,filename,savepath):
"""get img from mongdb by filename
"""
if len(savepath) < 1:
raise TypeError("dir must be an string of directory")
dataout=self.__imgfs.get_version(filename)
try:
imgout=open(savepath,'wb')
data=dataout.read()
imgout.write(data)
finally:
imgout.close()

使用示例:也可以将数据库连接写在类内部

 from pymongo import Connection
import mongoImg filedir=r'D:\image'
con = Connection()
db = con.imgdb
imgmongo=mongoImg.mongoImg(db,filedir)
imgmongo.insert()

感觉mongodb存储影像切片还是蛮快的,1w多个图片,大约100-200秒左右。

tip:

gridfs.GridFS.put 函数

put(data, **kwargs)
Put data in GridFS as a new file. Equivalent to doing: try:
f = new_file(**kwargs)
f.write(data)
finally
f.close()

在存储读取图像时,犯了低级错误,将open得到的file实例当做数据存储,读取的时候怎么也读不出数据。。。囧

另外以字节流形式读取图像数据比较适合。

pipe = open('/dev/input/js0','rb')

如果以str形式存储的话,可能会出现UnicodeDecodeError错误,貌似是因为图像数据有些超出了python默认编码的存储区间。

ps:初学python 数据库操作也忘得差不多 欢迎大家批评和指正~

mongodb python image 图像存储读取的更多相关文章

  1. mysql python image 图像存储读取

    最近做一些数据库调研的工作,目标是实现影像更快的入库.出库.查询,并实现并行访问等操作. 将结果总结成一个mysqlImg类. 关于mongodb的图像存储,参见http://www.cnblogs. ...

  2. python编码与存储读取数据(数组字典)

    Camp时在python2的编码上坑了不少. 理解pyhon2的编码 python2字符串类型只有两种: str类型:b'xxx'即是str类型, 是编码后的类型,len()按字节计算 unicode ...

  3. Python下opencv使用笔记(一)(图像简单读取、显示与储存)

    写在之前 从去年開始关注python这个软件,途中间间断断看与学过一些关于python的东西.感觉python确实是一个简单优美.easy上手的脚本编程语言,众多的第三方库使得python异常的强大. ...

  4. Scala与Mongodb实践2-----图片、日期的存储读取

    目的:在IDEA中实现图片.日期等相关的类型在mongodb存储读取 主要是Scala和mongodb里面的类型的转换.Scala里面的数据编码类型和mongodb里面的存储的数据类型各个不同.存在类 ...

  5. 浅析MongoDB数据库的海量数据存储应用

    [摘要]当今已进入大数据时代,特别是大规模互联网web2.0应用不断发展及云计算所需要的海量存储和海量计算发展,传统的关系型数据库已无法满足这方面的需求.随着NoSQL数据库的不断发展和成熟,可以较好 ...

  6. VTK序列图像的读取[转][改]

    医学图像处理的应用程序中,经常会碰到读取一个序列图像的操作.比如CT.MR等所成的图像都是一个切面一个切面地存储的,医学图像处理程序要处理这些数据,第一步当然是把这些数据从磁盘等外部存储介质中导入内存 ...

  7. MongoDB的地埋空间数据存储、空间索引以及空间查询

    一.关于MongoDB 在众多NoSQL数据库,MongoDB是一个优秀的产品.其官方介绍如下: MongoDB (from "humongous") is a scalable, ...

  8. Python 基于Python从mysql表读取千万数据实践

    基于Python 从mysql表读取千万数据实践   by:授客 QQ:1033553122 场景:   有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...

  9. mongoDB python 操作

    mongoDB python 操作 import pymongo mongo_client = pymongo.MongoClient(host="127.0.0.1",port= ...

随机推荐

  1. Raspberry Pi 3 --- identify the version of linux kernal file

    open it

  2. EasyUI-Datagrid 中formatter和group-formatter的使用

    1.在表格属性设置函数那块写以下内容: groupFormatter:function(value,rows){ //这里可以看到每一条导入表格中的数据,可以返回group的总结值 }, column ...

  3. 基本药目录sop

    http://db.yaozh.com/basicdir 基本药物 编辑 "基本药物"的概念, 由世界卫生组织于1977年提出,指的是能够满足基本医疗卫生需求,剂型适宜.保证供应. ...

  4. HTML学习笔记——列表和table

    1>有序列表.无序列表和自定义列表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  5. Jasper(物联网网络支撑平台公司)的技术为什么这么牛逼?

    Jasper在这个行业积累了十几年,合作的运营商超过30个,合作的行业大咖包括了通用.空客.宝马.特斯拉等几千个行业龙头,还是有很多积累下来的优势的. 一是,Jasper通过积累下来的行业应用经验,针 ...

  6. texlive2015+texstudio

    编译器 texlive2015 编辑器 texstudio

  7. json最简单的跨域

    html代码 <html> <head> <title>index.html</title> <script type="text/ja ...

  8. Robot Framework--02 菜单栏&工具栏

    转自:http://blog.csdn.net/tulituqi/article/details/7584795 我把RIDE的界面大致分了四个区域:菜单栏.工具栏.案例及资源区.工作区,如下图 菜单 ...

  9. apache外网不能访问分析与解决方法

    apache安装好以后,在本机可以用:http://localhost 或者 http://127.0.0.1进行访问,但是,在外网(相对本机来说的,局域网也算)不能访问. 这种情况可以分为两个问题, ...

  10. The length of the string value exceeds the length configured in the mapping/parameter.

    在NHibernate 3.3 中存储的字符串太长,会抛异常:The length of the string value exceeds the length configured in the m ...