美文网首页我爱编程
python 储存图片小图片

python 储存图片小图片

作者: jianmoumou | 来源:发表于2018-04-12 16:49 被阅读0次
import pymongo
import bson.binary
from pymongo import MongoClient
from cStringIO import StringIO


def insertFile():
    client = MongoClient('localhost', 27017)
    # 获得一个database
    db = client['images']
    # 获得一个collection
    coll = db['images']
    filename = r'C:\Users\ww\Downloads\1.png'.decode('utf-8')
    with open(filename, 'rb') as myimage:
        content = StringIO(myimage.read())
        coll.save(dict(
            content=bson.binary.Binary(content.getvalue()),
            filename='hehe.jpg'
        ))


def getFile():
    client = MongoClient('localhost', 27017)
    # 获得一个database
    db = client['images']
    # 获得一个collection
    coll = db['images']
    data = coll.find_one({'filename': 'hehe.jpg'})
    out = open(r'C:\Users\ww\Downloads\6.png'.decode('utf-8'), 'wb')
    out.write(data['content'])
    out.close()


getFile()

相关文章

网友评论

    本文标题:python 储存图片小图片

    本文链接:https://www.haomeiwen.com/subject/gnkfkftx.html