您好,登錄后才能下訂單哦!
小編給大家分享一下mongodb存圖片的方法,相信大部分人都還不怎么了解,因此分享這邊文章給大家學(xué)習(xí),希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去學(xué)習(xí)方法吧!
利用mongodb保存圖片通常有兩種方法,一種是將圖片數(shù)據(jù)轉(zhuǎn)化為二進(jìn)制作為字典的鍵值對(duì)進(jìn)行保存,另一種是利用mongodb提供的
GridFS進(jìn)行保存,兩者各有利弊。性能方面的優(yōu)劣未曾測(cè)試,無(wú)法進(jìn)行評(píng)價(jià),此處僅對(duì)兩種方式進(jìn)行介紹,若有徹知者還望指教。
下面以如下數(shù)據(jù)作為示例進(jìn)行介紹:
數(shù)據(jù)示例
dic = { "owner_name" : "samssmilin", "photo_id" : "602880671", "tags" : "", "longitude" : "-121.106479", "height" : "766", "datetaken" : "2004-01-17 21:05:35", "width" : "1024", "length" : 38141, "photo_title" : "Dad and Elijah", "latitude" : "35.565222", "photo_url" : "https://farm2.staticflickr.com/1063/602880671_c2f4511ef4_b.jpg", "dateupload" : "1075355967", "owner_id" : "45365637@N00" }
一、GridFS
GridFS將圖片數(shù)據(jù)與圖片屬性數(shù)據(jù)分開(kāi)保存,用chunks來(lái)保存圖片數(shù)據(jù),files保存屬性數(shù)據(jù),一個(gè)圖片file可能對(duì)應(yīng)多個(gè)chunks,每個(gè)
chunk的內(nèi)存大小固定(16M),若圖片數(shù)據(jù)大于chunk,則分為多個(gè)chunk保存,用同一個(gè)ObjectID關(guān)聯(lián),下載時(shí)自動(dòng)將多個(gè)chunk合
并為圖片數(shù)據(jù)。
上傳
from pymongo import MongoClient from gridfs import * import requests client = MongoClient('127.0.0.1', 27017) #連接mongodb db = client.photo #連接對(duì)應(yīng)數(shù)據(jù)庫(kù) #db.authenticate("username","passowd") fs = GridFS(db, collection="images") #連接collection data = requests.get(dic["photo_url"], timeout=10).content # 確認(rèn)數(shù)據(jù)庫(kù)中不存在此圖片之后再保存 if not fs.find_one({"photo_url":dic["photo_url"]}): fs.put(data, **dic) # 上傳成功后,photo數(shù)據(jù)庫(kù)下出現(xiàn)兩個(gè)collection,分別為: images.files, images.chunks12345678910111213
下載
from pymongo import MongoClient from gridfs import * client = MongoClient('127.0.0.1', 27017) #連接mongodb db = client.photo #連接對(duì)應(yīng)數(shù)據(jù)庫(kù) #db.authenticate("username","passowd") fs = GridFS(db, collection="images") #連接collection num = 1 for grid_out in fs.find(no_cursor_timeout=True): data = grid_out.read() # 獲取圖片數(shù)據(jù) outf = open('/home/%d.jpg'%num,'wb') outf.write(data) #存儲(chǔ)圖片 outf.close() if num%100000 == 0 metadata_file = open("/home/metadata%d.csv"%(num/100000+1), "ab") csv_writer = csv.writer(metadata_file,delimiter='\t') row = [grid_out.photo_title.encode('utf-8'), grid_out.uploadDate, grid_out.upload_date, \ grid_out.longitude, grid_out.latitude, grid_out.width, grid_out.height,\ grid_out.owner_name.encode('utf-8'), grid_out.photo_id, grid_out._id, grid_out.photo_url] csv_writer.writerow(row)12345678910111213141516171819
bson二進(jìn)制
這種方法將圖片數(shù)據(jù)作為鍵值對(duì)放入字典與屬性數(shù)據(jù)作為整體存入數(shù)據(jù)庫(kù)中。
上傳代碼如下:
from bson import binary from pymongo import MongoClient client = MongoClient('127.0.0.1', 27017) #連接mongodb db = client.photo #連接對(duì)應(yīng)數(shù)據(jù)庫(kù) image_collection = db.images data = requests.get(dic["photo_url"], timeout=10).content # 確認(rèn)數(shù)據(jù)庫(kù)中不存在此圖片之后再保存 if not image_collection.find_one({"photo_url":dic["photo_url"]}) dic["imagecontent"] = binary.Binary(data) image_collection.insert(dic)1234567891011
以上是mongodb存圖片的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。