[20:29:04] <altamish> Hi all, I am uploading an image on GridFS by fs.put(request.files['productImage']) , but when i try to download it using fs.get(_id) it returns bytes, how can i convert it to image or better yet get an image file?
[20:31:06] <GothAlice> altamish: GridFS knows not what the type of the data is, in general. If you know it's image data, you can funnel it into Pillow (an image library) to process it, or serve it straight back from the web app.
[20:31:36] <GothAlice> How you do this last part depends on the framework. In most you need to set a mime type on the response and return the bytes, or set the "body" of the response to bytes.
[20:33:50] <GothAlice> When accepting file uploads I make sure to also store the incoming file name and incoming mime type, and store them along with the reference to the GridFS file. Then, when serving the data back, my web has everything it needs and the code looks like:
[20:37:31] <altamish> thanks, i think i will try to set mime type while uploading
[20:37:53] <GothAlice> You can do so in the put: fs.put(someFileArgument.file, content_type=someFileArgument.mime_type)
[20:38:13] <GothAlice> (Remember to also do it in any .replace() calls, if applicable.)
[20:42:45] <altamish> fs.put(request.files['productImage'], content_type = image/jpeg) , is this ok? request.files['productImage'] is file from html form
[21:16:57] <altamish> GothAlice I used PIL to open the pictures, img = Image.open(im), im has bytes.. and it works.. thanks for the tip
[21:17:28] <GothAlice> No worries. Also remember to quote the mime type string…
[21:18:20] <altamish> yep i did already... however i am trying to pass img.show() into <img src> and it doesnt seem to work.. do you happen any way to do this