我已经成功地以二进制格式存储了mongdb中的图像文件。但是当我从mongodb获取图像时,我得到了相同的平面格式。但我需要这个图像文件。请有人帮忙
def retrieve(request):
db=pymongo.connection.Connection('localhost',27017).demo1
grid=gridfs.GridFS(db)
output=grid.get_last_version(filename='shiva.jpg')
return HttpResponse(output)
答案 0 :(得分:3)
您好我已经使用python成功插入并从mongodb中检索图像。
def insert_image(request):
with open(request.GET["image_name"], "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
print encoded_string
abc=db.database_name.insert({"image":encoded_string})
return HttpResponse("inserted")
def retrieve_image(request):
data = db.database_name.find()
data1 = json.loads(dumps(data))
img = data1[0]
img1 = img['image']
decode=img1.decode()
img_tag = '<img alt="sample" src="data:image/png;base64,{0}">'.format(decode)
return HttpResponse(img_tag)