Note that there are some explanatory texts on larger screens.

plurals
  1. POKineticJS toDataURL() gives incorrect padding error in Python
    primarykey
    data
    text
    <p>I have a base64 string that I've acquired from KineticJS toDataURL(). It's a valid base64 string. This fiddle shows that it is: <a href="http://jsfiddle.net/FakRe/" rel="nofollow">http://jsfiddle.net/FakRe/</a> </p> <p><strong>My Problem:</strong> I'm sending this dataURI up to my server, decoding using python, and saving on the server. However, I'm getting a padding error. Here's my code:</p> <pre><code>def base64_to_file(self, image_data, image_name): extension = re.search('data\:image\/(\w+)\;', image_data) if extension: ext = extension.group(1) image_name = '{0}.{1}'.format(image_name, ext) else: # it's not in a format we understand return None image_data = image_data + '=' #fix incorrect padding image_path = os.path.join('/my/image/path/', image_name) image_file = open(image_path, 'w+') image_file.write(image_data.decode('base64')) image_file.close() return image_file </code></pre> <p>I can get around this padding error by doing this at the top of my function:</p> <pre><code>image_data = image_data + '=' #fixes incorrect padding </code></pre> <p>After I add the arbitrary padding, it decodes successfully and writes to the filesystem. However, whenever I try and view the image, it doesn't render, and gives me the 'broken image' icon. No 404, just a broken image. <strong>What gives?</strong> Any help would be greatly appreciated. I've already spent way too much time on this as it is.</p> <p><strong>Steps to recreate (May be pedantic but I'll try and help)</strong></p> <ul> <li>Grab the base64 string from the JSFiddle</li> <li>Save it to a text file</li> <li>Open up the file in python, read in the data, save to variable</li> <li>Change '/path/to/my/image' in the function to anywhere on your machine</li> <li>Pass in your encoded text variable into the function with an name</li> <li>See the output</li> </ul> <p>Again, any help would be awesome. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload