python – 从base64代码保存图片

前端之家收集整理的这篇文章主要介绍了python – 从base64代码保存图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想将此图片保存为base64,但是当我尝试这样做时会出现以下错误

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

bace64:

这是由base64 JavaScript和Filereader生成的,与上传图像时生成代码完全相同.使用Ajax我发送了它.
这个base64自动生成.

  1. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAA
  2. AsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAl
  3. EtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsdioK2AfkIaKOg6OIisr74Xuja
  4. 9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrI
  5. sAHvgABeNMLCADATZvAMByH/w/qQplcAYCEAcB0kThLCIAUAEB6jkKmAEBGAYCdmCZTAKAEAGDLY2LjA
  6. FAtAGAnf+bTAICd+Jl7AQBblCEVAaCRACATZYhEAGg7AKzPVopFAFgwABRmS8Q5ANgtADBJV2ZIALC3A
  7. MDOEAuyAAgMADBRiIUpAAR7AGDIIyN4AISZABRG8lc88SuuEOcqAAB4mbI8uSQ5RYFbCC1xB1dXLh4oz
  8. kkXKxQ2YQJhmkAuwnmZGTKBNA/g88wAAKCRFRHgg/P9eM4Ors7ONo62Dl8t6r8G/yJiYuP+5c+rcEAAA
  9. OF0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gAAB45JREFUeNrEl0usXlUVx39r7X2+77uP3tsWgQawPKQVauQ1ICZGAolKcNAENAqJiUYmh
  48. pExxmjiACNEnZnoxJGBkOjAhlci4ECDYIhEFA2mLRVKpS320t7b3sf3OGevtRycc3v7AEUcuJOdc3K+v
  49. ff6fWv999prS0Tw/2wZ4ODrr298ERARBAEBTvMFIHj4LoIdQewgYrabdDIiDiAcqCf1qwtvLzAajTly5
  50. AiHDh3iwIEDVFXF5du30zTN+QDvpXn4JyL4bNJ0a+7lq3POUyklJQJzt9KUYVOavcBvRGSPu//xvXj3P
  51. wJExC0R8e3+YPDp6elpHfQHpJQQVURaF0WQwmNTsXLzeDy6eXZ29hvHjh178siRww9aKS+9X4DsZt/NV
  52. fXNubn5PDU1haoiACLr0epiBpKEnlZUVcXs7GzesmXLnXNzc59Kqg/s3bfvhznn9wYgCASbLOyhmZmZO
  53. +fn52knCyKCqnTvGwidF4DAPQBlMBhwxZVXzm69YOsP6qa55rnnnr9PREYpJc4MzXkAAQN3e2R206bdm
  54. +c3o7puOJ1j9Bzs7reUWhh3Qxzm5ua5++57vqyi+emnn/7Klq1bmipXRKduPWsZEcLtwcFgsHtu01xrW
  55. LQzLu9i/J1gWmDtQjU1NeCuu+764vbLt3/ntddeZ+nkEouLiywuLp4NEBGfVNGvzc7Ooiok1VZwIv/1/
  56. hYRUkokbU3MzExz7733fmswGNx26tQpJuOayXjSAjReKF5yT6vvzc1s0pnBNP3cJ+V8WnDvq4mQcqaf+
  57. 2St2HnVjt7n7/zc/W8cOjQ4/PZRDi+81Wrg4b/uYa4/e/sVmy/72LYLLmabXIzm1G231qUiCsqGN+R88
  58. XReBIcIJyIID8wML8bCyeP0ds7dUgZ++xsL/3g893IL8Oj+Z2YiuLum4cbLP8oNH/wIRRypFK0UzQnpd
  59. c+sSAJJimhLER6EOWEQxfFiRN0+vXGicXIof37zFV5Z2M/qxXbP8sFTT+TpKjKAhV8nyG2alJFNWGnWi
  60. ESbbFJCQ0lkRLTNBTl1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BoxAowpPN4v6DPv6JwuSskqx0+6MvWp5tTt23WfLV16fZXU04IIQITpCjjX2KjpwzMiFgt
  74. FooOIVodUGQEX5bTi79qiw+IPAnObcojY07CQ5Hfl6//QUq2XN9ntlZE1i04SgYKdr4t1cX7zK5EJ0O2
  75. oTcgnink2fLqaWH6mP3O/ELPfOuA0hEsEunz6oraoIVbOdn8taf3prnb61Eu8XaBTcANuasA7SHMSRgG
  76. M5TZfHgY82JB514OEFz7lVFIoJrzwBYb8eoqSPmr9Hpr99Rbf3q1WnqolacceaN4MxiCAgSwjicvTZce
  77. 6Kc+PV+H/0I+F06b+y/AQjgBA0CLEZhgF53bZr+0o266Y4defDhebJWIqQOwQjqCJaiiX02OvqSrf7hF
  78. V/bAzwFLK3X0/J+AIbhKMJKe4pdOoPedKFWN1wg1VV90XmCNMKGx70cXYh634R4GdgLrMpZxfr/AJAQ6
  79. i7JtLKjB8wDM500GmC56/FONeu7AfxrAL03hO3dzqKDAAAAAElFTkSuQmCCNDcwNw==
最佳答案
您需要删除前导数据:image / png; base64,.只有之后的所有内容都是base64数据.

也许这样的事情(从你的调用堆栈猜测):

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

编辑:回答Krasimir的评论

.split(‘,’)(在本例中)返回一个包含两个字符串的元组,即逗号(,)之前和之后的数据.第一个字符串是’data:image / png; base64′.第二个字符串是base64数据.左侧使用tuple unpacking将这两个字符串分配给名为_和b64data的变量. _只是一个变量,您可以将其重命名为任何有意义的变量. _ is typically used in tuple unpacking to signal “I don’t care about this part”.

猜你在找的Python相关文章