Saving base64 encoded image to file

1,686 views
Skip to first unread message

Mehul Ved

unread,
Jun 19, 2013, 4:51:35 AM6/19/13
to mu...@googlegroups.com
Hi,
  I am working with Flask where I receive JSON data from an app, which contains base64 JPEG image data. I am using the following code to save it to a file:

    data = json.loads(request.data)
    file = data['entry']['screenshots']
    filename = 'screenshot.jpeg'
    fh = open(filename, 'wb')
    fh.write(file.decode('base64'))
    fh.close()

But, the mimetype for the stored file shows as application/octet-stream. I even tried using base64 module and decodestring as well as b64decode but none of them work.

The data is sent out by https://github.com/nfroidure/BugMeBack and I am trying to capture it in https://github.com/mehulved/bugcatcher

Any clues on what is going wrong? 

--
With Regards,
Mehul Ved

Saket Choudhary

unread,
Jun 19, 2013, 5:05:19 AM6/19/13
to mu...@googlegroups.com


--
--
_________________________________________________
Mumbai Python Users Group - http://www.mumpy.org/
Mailing Group - http://groups.google.com/group/mumpy/
Membership Management - http://groups.google.com/group/mumpy/subscribe/
 
---
You received this message because you are subscribed to the Google Groups "mumpy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mumpy+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Vineet Naik

unread,
Jun 19, 2013, 5:09:07 AM6/19/13
to mu...@googlegroups.com
What mime type does the client set on the request? If it's application/octet-stream then `request.data` may not be the correct way to 
access the input data in Flask. Try reading from the input stream (Don't know how to do this in Flask but probably there would be a method on the request object)


--
--
_________________________________________________
Mumbai Python Users Group - http://www.mumpy.org/
Mailing Group - http://groups.google.com/group/mumpy/
Membership Management - http://groups.google.com/group/mumpy/subscribe/
 
---
You received this message because you are subscribed to the Google Groups "mumpy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mumpy+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Vineet Naik

Dhruv Baldawa

unread,
Jun 19, 2013, 5:19:36 AM6/19/13
to mu...@googlegroups.com
Just make a FileStorage object out of it and save it, that is what Flask uses for request.files. That should work IMO.


Uploading files in Flask

--
Dhruv Baldawa

Mehul Ved

unread,
Jun 19, 2013, 5:50:02 AM6/19/13
to mu...@googlegroups.com
On Wed, Jun 19, 2013 at 2:35 PM, Saket Choudhary <sak...@gmail.com> wrote:
Thanks, let me try that and see if it works. 

Mehul Ved

unread,
Jun 19, 2013, 5:52:32 AM6/19/13
to mu...@googlegroups.com
On Wed, Jun 19, 2013 at 2:39 PM, Vineet Naik <nai...@gmail.com> wrote:
What mime type does the client set on the request? If it's application/octet-stream then `request.data` may not be the correct way to 
access the input data in Flask. Try reading from the input stream (Don't know how to do this in Flask but probably there would be a method on the request object)

I receive the data as application/json. The other application sends me a json, inside which I have one of the fields is image.

Mehul Ved

unread,
Jun 19, 2013, 5:55:31 AM6/19/13
to mu...@googlegroups.com
On Wed, Jun 19, 2013 at 2:49 PM, Dhruv Baldawa <dhruvb...@gmail.com> wrote:
Just make a FileStorage object out of it and save it, that is what Flask uses for request.files. That should work IMO.


Uploading files in Flask

I haven't yet checked filestorage but I went through the fileuploads and from what I understand it won't work for me as I am not receiving data in multipart/form-data but as application/json. 

Vineet Naik

unread,
Jun 19, 2013, 5:58:34 AM6/19/13
to mu...@googlegroups.com
I think flask unserializes requests with json content type to request.json so you may want to try that instead of request.data
 

--
--
_________________________________________________
Mumbai Python Users Group - http://www.mumpy.org/
Mailing Group - http://groups.google.com/group/mumpy/
Membership Management - http://groups.google.com/group/mumpy/subscribe/
 
---
You received this message because you are subscribed to the Google Groups "mumpy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mumpy+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Vineet Naik

Vineet Naik

unread,
Jun 19, 2013, 6:00:56 AM6/19/13
to mu...@googlegroups.com
request.json should solve the problem, check this -- http://flask.pocoo.org/docs/api/#flask.Request.json
--
Vineet Naik

Mehul Ved

unread,
Jun 19, 2013, 6:04:41 AM6/19/13
to mu...@googlegroups.com
On Wed, Jun 19, 2013 at 2:35 PM, Saket Choudhary <sak...@gmail.com> wrote:
This worked perfectly. I am actually skipping the step of storing on the filesystem now. I will just store the image in the DB, if DB storage is configured. No need to save the file needlessly.

Mehul Ved

unread,
Jun 19, 2013, 6:06:14 AM6/19/13
to mu...@googlegroups.com
On Wed, Jun 19, 2013 at 3:30 PM, Vineet Naik <nai...@gmail.com> wrote:
request.json should solve the problem, check this -- http://flask.pocoo.org/docs/api/#flask.Request.json

I remember having tried request.json, which didn't work. But, maybe I was too sleepy and got it wrong. 
The other method has worked for now, but I'll try this as well as my primary reason to choose flask was to learn more about it. 

Dhruv Baldawa

unread,
Jun 19, 2013, 6:12:00 AM6/19/13
to mu...@googlegroups.com
This might work I guess:

import cStringIO
from werkzeug.datastructures import FileStorage


decoded_data = request.json['entry']['screenshots'].decode('base64')
file_data = cStringIO.StringIO(decoded_data)
file = FileStorage(file_data, filename='screenshot.jpg', mimetype='application/json')

if file and allowed_file(file.filename):
    filename = secure_filename(file.filename)
    file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    return redirect(url_for('uploaded_file',
                            filename=filename))

--
Dhruv Baldawa


--
Reply all
Reply to author
Forward
0 new messages