mongodb python image 图像存储读取
来源:互联网 发布:ubuntu文件大小怎么看 编辑:程序博客网 时间:2024/05/16 10:32
最近做一些数据库调研的工作,目标是实现影像更快的入库、出库、查询,并实现并行访问等操作。
将结果总结成一个mongoImg类,也算是小结吧。
1 ''' 2 Created on 2013-8-6 3 class mongoInsert 4 @author: tree 5 ''' 6 __metaclass__ = type 7 8 import os 9 from pymongo.database import Database10 import time11 import gridfs12 13 class mongoImg(object):14 """mongoInsert is a class for inserting document15 16 17 """18 def __init__(self, database, dir):19 """Create a new instance of :class:mongoInsert20 :Parameters:21 - `database`: database to use22 - `dir` : directory of document 23 """24 if not isinstance(database, Database):25 raise TypeError("database must be an instance of Database")26 if len(dir) < 1:27 raise TypeError("dir must be an string of directory")28 29 # self.__con = Connection()30 self.__imgdb = database31 self.__imgfs = gridfs.GridFS (self.__imgdb)32 self.__dir = dir33 self.__filelist=[]34 35 #save filepath in list.txt36 def __dirwalk(self,topdown=True):37 """traverse the documents of self.__dir and save in self.__filelist38 """39 sum=040 self.__filelist.clear()41 42 for root,dirs,files in os.walk(self.__dir,topdown):43 for name in files:44 sum+=145 temp=os.path.join(root,name)46 self.__filelist.append(temp)47 print(sum)48 49 #insert image 50 def insert(self):51 """insert images in mongodb52 """53 self.__dirwalk()54 55 tStart = time.time() 56 for fi in self.__filelist: 57 with open (fi,'rb') as myimage:58 data=myimage.read() 59 self.__imgfs.put(data, content_type = "jpg", filename =fi)60 61 tEnd =time.time ()62 print ("It cost %f sec" % (tEnd - tStart))63 64 #get image by filename65 def getbyname(self,filename,savepath):66 """get img from mongdb by filename67 """68 if len(savepath) < 1:69 raise TypeError("dir must be an string of directory")70 dataout=self.__imgfs.get_version(filename)71 try:72 imgout=open(savepath,'wb')73 data=dataout.read()74 imgout.write(data)75 finally:76 imgout.close()77
使用示例:也可以将数据库连接写在类内部
1 from pymongo import Connection2 import mongoImg3 4 filedir=r'D:\image'5 con = Connection()6 db = con.imgdb7 imgmongo=mongoImg.mongoImg(db,filedir)8 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) finallyf.close()
在存储读取图像时,犯了低级错误,将open得到的file实例当做数据存储,读取的时候怎么也读不出数据。。。囧
另外以字节流形式读取图像数据比较适合。
pipe = open('/dev/input/js0','rb')
如果以str形式存储的话,可能会出现UnicodeDecodeError错误,貌似是因为图像数据有些超出了python默认编码的存储区间。
ps:初学python 数据库操作也忘得差不多 欢迎大家批评和指正~
0 0
- mongodb python image 图像存储读取
- mysql python image 图像存储读取
- python读取image
- Opencv图像读取,存储
- python image 图像拼接显示
- Python pillow Image图像处理
- python-mongodb存储文件
- Python + Mongodb数据存储
- python-mongodb存储文件
- python读取MNIST image数据
- python读取MNIST image数据
- mongodb存储和读取数据
- mongoDB的简单存储读取
- Mongodb存储读取Word文档
- Python读取图像
- [Python开发] python使用Image合并图像
- 二 读取、显示、存储图像
- 存储、读取二进制的图像
- MongoDB—— 读操作 Core MongoDB Operations (CRUD)
- MongoDB—— 写操作 Core MongoDB Operations (CRUD)
- Mongodb——GridFS
- URL中 & 、 ? 、 # 的作用
- 2013/07/11 中科院软件所就业讲座总结
- mongodb python image 图像存储读取
- Mysql BLOB和TEXT类型
- mysql python image
- MySQL Python教程(1)
- MySQL Python教程(2)
- MySQL Python教程(3)
- MySQL Python教程(4)
- 【SDOI2012】【BZOJ2705】【Longge的问题】【题解】【数论】
- mysql python image 图像存储读取