0

I have the following code that creates a thumbnail from a request to a url:

r = requests.get(image_url, stream=True, headers=headers)
size = 500, 500
img = Image.open(r.raw)
thumb = ImageOps.fit(img, size, Image.ANTIALIAS)

At this point I would like to store the image inside a mongo document like so:

photo = {
    'thumbnail': img,
    'source': source,
    'tags': tags,
    'creationDate': datetime.now(),
}

Obviously that won't work so what kind of transformation do I need to apply before I can do this?

9
  • This is a bad idea, never ever ever... ever... store an image in a database... just don't. Store a reference to a url of its location...
    – Aydin
    Commented Feb 7, 2016 at 10:52
  • It's a ~16KB thumbnail, why not store it in binary? do you actually have a good reason or are you just repeating what you've heard?
    – davegri
    Commented Feb 7, 2016 at 11:08
  • Im telling you from my first hand experience
    – Aydin
    Commented Feb 7, 2016 at 11:11
  • You still haven't given me a good reason not to store a 16kb file in binary in a database. mongoose documents can be up to 16MB in size
    – davegri
    Commented Feb 7, 2016 at 11:31
  • Typing from a phone was a pain, the reason is simple, performance penalties... admittedly 16kb is rather small for an image, but it's still quite large when in comparison to 16kb of text. Whenever you need to serve an image you need to first extract it from the data store, and then proceed to serve it to your user. Since they're thumbnails, you're going to be doing this tens of times on each request, each of those requests add up... Say you gathered the lot in one request, it's still a large transaction for a single (and simple) request.
    – Aydin
    Commented Feb 7, 2016 at 11:34

1 Answer 1

2

Okay here are my thoughts on this (I am not certain it will work though; some thoughts adopted from here).

I think you can achieve what you need using the Binary BSON type in pymongo library. Try loading the image in binary. Say using PILLOW (pil.image) or

image_file = open('1.bmp', 'rb')

or as

image_file = StringIO(open("test.jpg",'rb').read())

and then send it to Binary(image_file) type in pymongo

Binary_image_file = Binary(image_file) #pymongo libary

Then do a normal insert in mongo.

To read. do a normal find(). Then load the value from key and convert the data stored to image as:

image_data = StringIO.StringIO(Stringio_image_file)
image = Image.open(image_data)

I hope that helps a little. (also you could go with Aydin base64 proposition).

All the best.

1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.