日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

python gridfs_python 将图片存入mongodb,读取图片,gridfs模块

發(fā)布時(shí)間:2025/3/20 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python gridfs_python 将图片存入mongodb,读取图片,gridfs模块 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

導(dǎo)入圖片

引入模塊,其中g(shù)ridfs模塊不需要單獨(dú)安裝,引入了pymongo即可直接引入

from pymongo import MongoClient

from gridfs import *

import os

#鏈接mongodb

client=MongoClient('localhost',27017)

#取得對(duì)應(yīng)的collection

db=client.image

#本地硬盤(pán)上的圖片目錄

dirs = 'e:\cs'

#列出目錄下的所有圖片

files = os.listdir(dirs)

#遍歷圖片目錄集合

for file in files:

#圖片的全路徑

filesname = dirs + '\\' + file

#分割,為了存儲(chǔ)圖片文件的格式和名稱(chēng)

f = file.split('.')

#類(lèi)似于創(chuàng)建文件

datatmp = open(filesname, 'rb')

#創(chuàng)建寫(xiě)入流

imgput = GridFS(db)

#將數(shù)據(jù)寫(xiě)入,文件類(lèi)型和名稱(chēng)通過(guò)前面的分割得到

insertimg=imgput.put(datatmp,content_type=f[1],filename=f[0])

datatmp.close()

print("js")

創(chuàng)建成功后,會(huì)在集合中生成fs.flies和fs.chunks

導(dǎo)出圖片

from pymongo import MongoClient

from gridfs import *

client=MongoClient('localhost',27017)

db=client.image

#給予girdfs模塊來(lái)寫(xiě)出,其中collection為上一步生成的,我不知道怎么該名稱(chēng)。實(shí)際上是由fs.flies和fs.chunks組成

gridFS = GridFS(db, collection="fs")

count=0

for grid_out in gridFS.find():

count+=1

print(count)

data = grid_out.read() # 獲取圖片數(shù)據(jù)

outf = open('cs.jpg','wb')#創(chuàng)建文件

outf.write(data) # 存儲(chǔ)圖片

outf.close()

---------------------

作者:a873054267

總結(jié)

以上是生活随笔為你收集整理的python gridfs_python 将图片存入mongodb,读取图片,gridfs模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。