Decoding base64 from POST to use in PIL

前端 未结 4 1713
感动是毒
感动是毒 2020-12-08 04:43

I\'m making a simple API in Flask that accepts an image encoded in base64, then decodes it for further processing using Pillow.

I\'ve looked at some examples (1, 2,

相关标签:
4条回答
  • 2020-12-08 04:47

    I'm using the ways mentioned above. But I'm getting this error at Image.open: cannot identify image file <_io.BytesIO object at 0x11c267590>.

    This is my code:

    image_data = re.sub('^data:image/.+;base64,', '', testBase64)
    data = Image.open(io.BytesIO(base64.b64decode(image_data)))
    

    Any ideas/ suggestions?

    0 讨论(0)
  • 2020-12-08 04:52

    There is a metadata prefix of data:image/jpeg;base64, being included in the img field. Normally this metadata is used in a CSS or HTML data URI when embedding image data into the document or stylesheet. It is there to provide the MIME type and encoding of the embedded data to the rendering browser.

    You can strip off the prefix before the base64 decode and this should result in valid image data that PIL can load (see below), but you really need to question how the metadata is being submitted to your server as normally it should not.

    import re
    import cStringIO
    from PIL import Image
    
    image_data = re.sub('^data:image/.+;base64,', '', data['img']).decode('base64')
    image = Image.open(cStringIO.StringIO(image_data))
    
    0 讨论(0)
  • 2020-12-08 05:10

    You should try something like:

    from PIL import Image
    from io import BytesIO
    import base64
    
    data['img'] = '''R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLl
    N48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==''' 
    
    im = Image.open(BytesIO(base64.b64decode(data['img'])))
    

    Your data['img'] string should not include the HTML tags or the parameters data:image/jpeg;base64 that are in the example JSFiddle.

    I've changed the image string for an example I took from Google just for readability purposes.

    0 讨论(0)
  • 2020-12-08 05:12

    Sorry for necromancy, but none of the answers worked completely for me. Here is code working on Python 3.6 and Flask 0.13.

    Server:

    from flask import Flask, jsonify, request
    from io import BytesIO
    from web import app
    import base64
    import re
    import json
    from PIL import Image
    
    @app.route('/process_image', methods=['post'])
    def process_image():
        image_data = re.sub('^data:image/.+;base64,', '', request.form['data'])
        im = Image.open(BytesIO(base64.b64decode(image_data)))
        return json.dumps({'result': 'success'}), 200, {'ContentType': 'application/json'}
    

    Client JS:

    // file comes from file input
    var reader = new FileReader();
    reader.onloadend = function () {
        var fileName = file.name;
        $.post('/process_image', { data: reader.result, name: fileName });
    };
    reader.readAsDataURL(file);
    
    0 讨论(0)
提交回复
热议问题