pythonは画像の小さな画像を保存します

1009 ワード

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()