1

I have been trying to save canvas data as image (png) using python and ajax jquery. But, getting blank image. Here is the codes below: JavaScript:

var dataimg=document.getElementById('canvas-data');
var dataURL=dataimg.toDataURL();
$("#save_to_server").click(function(){
    $.ajax({
        type:"POST",
        url:"save-canvas",
        data:{
            imageBase64:dataURL
            }
        }).done(function(o){
            console.log('saved');
        });
    });

Python:

@app.route('/save-canvas', methods=['POST'])
def save_canvas():
    image_b64=request.values[('imageBase64')]
    imgstr=re.search(r'data:image/png;base64,(.*)',image_b64).group(1)
    output=open('output.png', 'wb')
    decoded=base64.b64decode(imgstr)
    output.write(decoded)
    output.close()

Output: enter image description here followed this and this answers. Can someone please tell what I am doing wrong? Thanks

Community
  • 1
  • 1
MHR
  • 53
  • 11

0 Answers0