Request for Python dataurl flask from url - to decode base64 image

I can't figure out how to request a decoded base64 image string from the url that was generated with my javascript.

I am resizing the input image by drawing it on the canvas, now I need the image that is in the canvas to be sent to my python application. Here I create a dataurl that also works fine and sends it to my python using:

var dataurl = canvas.toDataURL("image/png"); $.getJSON("http://localhost:5000/profil/unternehmen-bearbeiten", { a: dataurl }); 

In my console, I see that dataurl is in the url, but I cannot get it:

enter image description here

I use this code to request an image string (I tried many approaches, but I get either Bad Request, or my test print is None):

 #data_url = request.values['a'] data_url = request.args.get('a') print data_url content = data_url.split(';')[1] image_encoded = content.split(',')[1] body = base64.decodebytes(image_encoded.encode('utf-8')) # Then do whatever you want print body, type(body) 

Here is my previous question with a lot of code for resizing an image, there I used the AJAX approach, but had the same problem (should not be important).

EDIT:

I used print request.url to check the url and there is no request argument a , although I see it in the console, so this is its None . I am trying to get it to work with request.args.get('a')

EDIT

I managed to extract the request argument in the new view, but now I understand even less, not all printouts are shown, and I get the Bad Request site and no errors. I also use decodestring instead of decodebytes

New JS (only resized added):

 $.getJSON("http://localhost:5000/profil/unternehmen-bearbeiten/resize-image", { a: dataurl }); 

Here's a view in python:

 @app.route('/profil/unternehmen-bearbeiten/resize-image') def resize_image(): data_url = request.values['a'] print "extern" print data_url content = data_url.split(';')[1] print "the content ", content image_encoded = content.split(',')[1] print "the image ", image_encoded body = base64.decodestring(image_encoded.encode('utf-8')) print "decoded", type(body) return body #return jsonify(result=a + b) 

For an unknown reason, only three fingerprints are displayed in my console: enter image description here

And in my code, where I need an image, I now use resize_image (). The main problem here is atm. Bad Request . Later I will need the file name and file, so I can work with the image of the canvas.

EDIT

Bad Request only happens if I use my resize_image() function in a view where I need to replace form.company_logo_image_path.data new changed image from the canvas, so basically, how to use the new image?

EDIT

I did an additional test to verify that the modified image is stored in the body the resize_image() function, and it works well, this saves the correct image on my computer:

 filename = 'some_image.jpg' with open(filename, 'wb') as f: print "written" f.write(body) 

I just need to provide this new image of my upload function in a different view where form.company_logo_image_path.data is replaced with it.

+1
source share

Source: https://habr.com/ru/post/1265485/


All Articles