0

I saved a picture into mongodb as binary file, but when I read it and use pillow to load this picture, It's not the picture I want. here is my code

from pymongo import MongoClient
from PIL import Image
client = MongoClient('', 27017) 
db = client.mytest [enter image description here][1]
my_set = db.test_set
def save_pic():
    with open(r'C:\Users\Administrator\Desktop\照片\wallhaven-634613.jpg', 
    'rb')  as f:
        data = f.read()
        my_set.insert({"pic":data})


def read_pic():
    data = my_set.find({"id": "dk"})[0]
    Image.frombytes("RGB", (100,100), data["pic"]).show()

read_pic()

image is under here

enter image description here

  • How did you load the picture into `mongodb` in the first place? – Mark Setchell Jul 27 '18 at 16:56
  • def save_pic(): with open(r'C:\Users\Administrator\Desktop\照片\wallhaven-634613.jpg', 'rb') as f: data = f.read() my_set.insert({"pic":data}) – user9617730 Jul 28 '18 at 02:56
  • ["Note that this function decodes pixel data only, not entire images. If you have an entire image in a string, wrap it in a BytesIO object, and use open() to load it."](http://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.frombytes) – user2357112 Jul 28 '18 at 03:04
  • Another answer that is worth looking at is [this](https://stackoverflow.com/a/20857415/6649050) – doodhwala Jul 28 '18 at 03:09

0 Answers0