Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save picture from base64 code

I want to save this picture as base64, but when I try to do this the following error appears:

Traceback (most recent call last):
  File "E:\Programs\Python\lib\site-packages\django\core\handlers\base.py", line
 116, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "E:\RSYearWork\ServerRS\Views\TestForAjaxView.py", line 27, in addAjax
    f.write(decodestring(request.POST['UplaodFile']))
  File "E:\Programs\Python\lib\base64.py", line 321, in decodestring
    return binascii.a2b_base64(s)
Error: Incorrect padding

bace64:

This is generated by base64 JavaScript and Filereader exactly that when uploading image to generate the code. With Ajax I sent it. This base64 generated automatically.

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAA
AsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAl
EtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja
9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrI
sAHvgABeNMLCADATZvAMByH/w/qQplcAYCEAcB0kThLCIAUAEB6jkKmAEBGAYCdmCZTAKAEAGDLY2LjA
FAtAGAnf+bTAICd+Jl7AQBblCEVAaCRACATZYhEAGg7AKzPVopFAFgwABRmS8Q5ANgtADBJV2ZIALC3A
MDOEAuyAAgMADBRiIUpAAR7AGDIIyN4AISZABRG8lc88SuuEOcqAAB4mbI8uSQ5RYFbCC1xB1dXLh4oz
kkXKxQ2YQJhmkAuwnmZGTKBNA/g88wAAKCRFRHgg/P9eM4Ors7ONo62Dl8t6r8G/yJiYuP+5c+rcEAAA
OF0ftH+LC+zGoA7BoBt/qIl7gRoXgugdfeLZrIPQLUAoOnaV/Nw+H48PEWhkLnZ2eXk5NhKxEJbYcpXf
f5nwl/AV/1s+X48/P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H/0I+F06b+y/AQjgBA0CLEZhgF53bZr+0o266Y4defDhebJWIqQOwQjqCJaiiX02OvqSrf7hF
V/bAzwFLK3X0/J+AIbhKMJKe4pdOoPedKFWN1wg1VV90XmCNMKGx70cXYh634R4GdgLrMpZxfr/AJAQ6
i7JtLKjB8wDM500GmC56/FONeu7AfxrAL03hO3dzqKDAAAAAElFTkSuQmCCNDcwNw==
like image 301
Krasimir Avatar asked Jan 29 '13 16:01

Krasimir


Video Answer


1 Answers

You need to remove the leading data:image/png;base64,. Only everything after the , is base64 data.

Maybe something like this (guessing from your call stack):

_, b64data = request.POST['UplaodFile'].split(',') # [sic]
f.write(decodestring(b64data))

Edit: To answer Krasimir's comment:

.split(',') (in this case) returns a tuple containing two strings, the data before and after the comma (,). The first string is 'data:image/png;base64'. The second string is the base64 data. The left-hand side is using tuple unpacking to assign those two strings to variables named _ and b64data. _ is just a variable, you can rename it to whatever makes sense. _ is typically used in tuple unpacking to signal "I don't care about this part".

like image 60
Jon-Eric Avatar answered Sep 18 '22 10:09

Jon-Eric