我正在尝试通过 python 代码将 base64 字符串转换为图像,但我得到了binascii.Error:填充不正确我已经和我的solution https://stackoverflow.com/a/9807138但他们只建议检查字符串长度是否能被 4 整除,如果不能被 4 整除,可以通过在 base64 编码的字符串末尾添加“=”字符来使其被 4 整除。
请帮忙。
Python代码: https://drive.google.com/file/d/0B0iJEAXqbN2Ickg5bEJ6Q1pZcVU/view?usp=sharing(请检查驱动器中的代码以获得更多可见性)
import base64
strOne= 'data:image/png;base64,iVBORw0KGgoAAAANSU...string has 200000 character thats why I couldn t paste'
print 'strOne Length',len(strOne)
print 'StrOne Length is completely divisible by 4 (len%4),(len/4):', len(strOne)%4,len(strOne)/4
with open("imageToSave.png", "wb") as fh:
fh.write(strOne.strip().decode('base64'))
output:
strOne Length 200000
StrOne Length is completely divisible by 4 (len%4),(len/4): 0 50000
Traceback (most recent call last):
File "/tests.py", line 13, in <module>
fh.write(strOne.strip().decode('base64'))
File "/usr/lib/python2.7/encodings/base64_codec.py", line 42, in base64_decode
output = base64.decodestring(input)
File "/usr/lib/python2.7/base64.py", line 328, in decodestring
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding