1

From the client's POST submission, we are successfully receiving an image in the following format, data:image/jpeg;base64,/9j/.... and an image file is being generated by the following code:

@app.route('/submission', methods=('GET', 'POST'))
def submission():
    
    if request.method == 'POST':
        
        raw_image = request.form['file']
        #Problem Starts Here
        # this doesn't work either: 
        # raw_image = raw_image.replace("data:image/jpeg;base64,/9j/", '');
        with open(UPLOADED_IMAGES_DEST+'/image.jpeg', 'wb') as fh:
            fh.write(base64.decodebytes(raw_image))
        #Problem Ends Here
        return jsonify(request.form)
        
    return render_template('submission.html')

However, the image cannot be opened as it's an "Invalid or Unsupported Format". I'm trying to improve the conversion process so it will produce a viewable JPEG image.

Beginning of raw_image:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCADwAUADASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABQYEBwIDCAEACf/EAEwQAAEDAwMCBAQEAwUFBQQLAAECAxEABAUGEiExQQcTUWEIInGBFDKRoRVCUiOxwdHwJGJyouEJFjOSwhdDgvEmU2ODk7LDxNLU4v/EABsBAAMBAQEBAQAAAAAAAAAAA...

4
  • 2
    can you please, print the beginning of raw_image and image.jpeg file ? Commented Jul 4, 2018 at 1:25
  • I've added the raw_image but upon closer inspection, it looks like the file being created is empty and is 0 bytes. Commented Jul 4, 2018 at 15:43
  • I think you need to pass only the stuff following base64, to decodebytes(). Commented Jul 4, 2018 at 15:48
  • In case unclear, I mean you should pass /9j.... onwards to decodebytes(), but don't search for /9j because it will be different if you get a PNG file, for example. Commented Jul 4, 2018 at 16:00

1 Answer 1

4

I took your data, and decoded it, it looks quite close to the JPEG header I took from the real file (picture below). Don't see any problem, except, you have to start decoding from /9j/4AA... onwards.

>>> a = '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCADwAUADASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABQYEBwIDCAEACf/EAEwQAAEDAwMCBAQEAwUFBQQLAAECAxEABAUGEiExQQcTUWEIInGBFDKRoRVCUiOxwdHwJGJyouEJFjOSwhdDgvEmU2ODk7LDxNLU4v/EABsBAAMBAQEBAQAAAAAAAAAA'
>>> base64.b64decode(a)
'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05

This is a hex dump from the real JPEG picture of mine.

enter image description here

You have to remove data:image/jpeg;base64,, but don't go beyond that -- this will break the image data.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. Now I get an error on base64.decodebytes(raw_image) : TypeError: expected bytes-like object, not str. I tried to switch to .decodestring() but received the same error. Is my use of raw_image = raw_image.replace("data:image/jpeg;base64,", ''); ruining the data?
@dadiletta please, carefully read my answer. if you have a string, use base64.b64decode()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.