mongodb python image 图像存储读取
最近做一些数据库调研的工作,目标是实现影像更快的入库、出库、查询,并实现并行访问等操作。
将结果总结成一个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 图像存储读取的更多相关文章
- mysql python image 图像存储读取
最近做一些数据库调研的工作,目标是实现影像更快的入库.出库.查询,并实现并行访问等操作. 将结果总结成一个mysqlImg类. 关于mongodb的图像存储,参见http://www.cnblogs. ...
- python编码与存储读取数据(数组字典)
Camp时在python2的编码上坑了不少. 理解pyhon2的编码 python2字符串类型只有两种: str类型:b'xxx'即是str类型, 是编码后的类型,len()按字节计算 unicode ...
- Python下opencv使用笔记(一)(图像简单读取、显示与储存)
写在之前 从去年開始关注python这个软件,途中间间断断看与学过一些关于python的东西.感觉python确实是一个简单优美.easy上手的脚本编程语言,众多的第三方库使得python异常的强大. ...
- Scala与Mongodb实践2-----图片、日期的存储读取
目的:在IDEA中实现图片.日期等相关的类型在mongodb存储读取 主要是Scala和mongodb里面的类型的转换.Scala里面的数据编码类型和mongodb里面的存储的数据类型各个不同.存在类 ...
- 浅析MongoDB数据库的海量数据存储应用
[摘要]当今已进入大数据时代,特别是大规模互联网web2.0应用不断发展及云计算所需要的海量存储和海量计算发展,传统的关系型数据库已无法满足这方面的需求.随着NoSQL数据库的不断发展和成熟,可以较好 ...
- VTK序列图像的读取[转][改]
医学图像处理的应用程序中,经常会碰到读取一个序列图像的操作.比如CT.MR等所成的图像都是一个切面一个切面地存储的,医学图像处理程序要处理这些数据,第一步当然是把这些数据从磁盘等外部存储介质中导入内存 ...
- MongoDB的地埋空间数据存储、空间索引以及空间查询
一.关于MongoDB 在众多NoSQL数据库,MongoDB是一个优秀的产品.其官方介绍如下: MongoDB (from "humongous") is a scalable, ...
- Python 基于Python从mysql表读取千万数据实践
基于Python 从mysql表读取千万数据实践 by:授客 QQ:1033553122 场景: 有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...
- mongoDB python 操作
mongoDB python 操作 import pymongo mongo_client = pymongo.MongoClient(host="127.0.0.1",port= ...
随机推荐
- MySQL安装问题
win8.1安装mysql5.6时遇到问题,主要出现最后一步配置卡在了start server这一步会弹窗配置时间过长. 在网上查找方式并尝试之后都不能解决问题,尝试了卸载软件,清理注册表中的mysq ...
- centos忘记root密码,重新设置的方法
今天重新装了一个centos6.6,好像root密码没有叫我设置吧,然后用虚拟机开始安装之前的密码登录显示失败,所以有了下面的彩蛋.....Helloween... 在虚拟机安装了Centos,今天要 ...
- mybatis int 类型判断<if>
如果数据类型是integer或者int,也就是数据类型的,在用<if>标签做动态语句的时候 不用判断是否为"''" <if test="sex != n ...
- FIFA halts 2026 bids amid scandal 国际足联在丑闻期间停止2026年足球世界杯申请
FIFA halts 2026 bids amid scandal 国际足联在丑闻期间停止2026年足球世界杯申请 But official insists 2018 Cup will stay in ...
- 纯css3实现旋转的太极图
效果图: 代码如下: <!DOCTYPE html> <html> <head lang="zh"> <meta charset=&quo ...
- 3秒后自动跳转页面【js】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- chmod 和 chown 的用法
一.chown 命令 用途:更改文件的所有者或组.命令由单词change owner组合而成. 使用示例: 1,更改文件的所有者: chown jim program.c 文件 program.c 的 ...
- Java之强引用、 软引用、 弱引用、虚引用
1.强引用 平时我们编程的时候例如:Object object=new Object();那object就是一个强引用了.如果一个对象具有强引用,那就类似于必不可少的生活用品,垃圾回收器绝不会回收它. ...
- 使用RawSocket进行网络抓包
aw socket,即原始套接字,可以接收本机网卡上的数据帧或者数据包,对与监听网络的流量和分析是很有作用的,一共可以有3种方式创建这种socket. 中文名 原始套接字 外文名 RAW SOCKET ...
- CSS学习笔记——选择器
选择器 当我们定义一条样式规则时候,这条样式规则会作用于网页当中的某些元素,而我们的规定的这些元素的规则就叫做选择器. 常用的选择器: 1.id选择器 #idname 2.类选择器 .classnam ...