Python TypeError: Incorrect padding when write to image

Python TypeError: Incorrect padding when write to image

我厌倦了这个错误。这是编码 base64 http://pastebin.com/16KSrNuL,我尝试使用此代码

解码为图像
wf = open('/Users/me/base.txt', 'w')
wf.write(data.get('base64'))
wf.close()

fp = open('/Users/me/base_result.png', 'wb')
fp.write(base64.b64decode(open('/Users/me/base.txt', 'rb').read()))
fp.close()

就我而言,我尝试 post 数据 json。

您需要删除前导字符串,即 data:image/png;base64, 以获得 base64 编码数据:

with open("/Users/me/base.txt") as f, open("/Users/me/base_result.png","wb") as out:
    out.write(f.read().split(",",1)[1].decode("base-64"))

当你这样做时,你会得到:

显然前导子字符串不是 base64 编码的。