如何将base64编码的文件转换为normal.jpg文件?

时间:2014-04-26 11:09:35

标签: python base64 decode encode

嗨,这是我的示例base64编码文件:

myencodeimage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC"

我想把jason.jpg作为这个编码文本。

g = open("out.jpg", "w")
g.write(base64.decodestring(myencodeimage))
g.close()

但我有一个" binascii.Error:填充错误"问题?

1 个答案:

答案 0 :(得分:2)

您尝试解码整个字符串,而不是仅解码base64编码数据。

myencodeimage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC"

尝试解码

myencodeimage = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC"

但请记住,图像位于PNG容器中,而不是压缩的JPEG图像,因此基本上您的操作系统无法在最坏的情况下调用正确的程序来打开图像。