保存base64图像python [英] save base64 image python

查看:755
本文介绍了保存base64图像python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Base64编码的python保存图像。这里的字符串是大到发布但这里是图像

I am trying to save an image with python that is Base64 encoded. Here the string is to large to post but here is the image

当被python收到时,最后2个字符是 == 虽然字符串没有格式化所以我这样做

And when received by python the last 2 characters are == although the string is not formatted so I do this

import base64
data = "data:image/png;base64," + photo_base64.replace(" ", "+")

然后我这样做

imgdata = base64.b64decode(data)
    filename = 'some_image.jpg'  # I assume you have a way of picking unique filenames
    with open(filename, 'wb') as f:
            f.write(imgdata)

但这导致此错误

Traceback (most recent call last):
  File "/var/www/cgi-bin/save_info.py", line 83, in <module>
    imgdata = base64.b64decode(data)
  File "/usr/lib64/python2.7/base64.py", line 76, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

一旦我也打印出字符串的长度数据:image / png; base64,已添加,空格替换为 + 它的长度为 34354 ,我尝试了一堆不同的图像,但是当我尝试打开保存的文件时,所有这些图像都说文件已损坏。

I also printed out the length of the string once the data:image/png;base64, has been added and the spaces replace with + and it has a length of 34354, I have tried a bunch of different images but all of them when I try to open the saved file say that the file is damaged.

发生了什么以及为什么文件损坏?

What is happening and why is the file corrupt?

谢谢

编辑

以下是一些也失败的base64

Here is some base64 that also failed

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBQTFRFA6b1q Ci5/f2lt/9yu3 Y8v2cMpb1/DSJbz5i9R2NLwfLrWbw m T8I8////////SvMAbAAAABB0Uk5T////////////////////AOAjXRkAAACYSURBVHjaLI8JDgMgCAQ5BVG3//9t0XYTE2Y5BPq0IGpwtxtTP4G5IFNMnmEKuCopPKUN8VTNpEylNgmCxjZa2c1kafpHSvMkX6sWe7PTkwRX1dY7gdyMRHZdZ98CF6NZT2ecMVaL9tmzTtMYcwbP y3XeTgZkF5s1OSHwRzo1fkILgWC5R0X4BHYu7t/136wO71DbvwVYADUkQegpokSjwAAAABJRU5ErkJggg==

这是我在我的python脚本接收来自POST请求

This is what I receive in my python script from the POST Request

注意我没有用+'替换空格

Note I have not replace the spaces with +'s

推荐答案

那里不需要添加 data:image / png; base64,之前,我尝试使用下面的代码,它工作正常。

There is no need to add data:image/png;base64, before, I tried using the code below, it works fine.

import base64
data = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBQTFRFA6b1q Ci5/f2lt/9yu3 Y8v2cMpb1/DSJbz5i9R2NLwfLrWbw m T8I8////////SvMAbAAAABB0Uk5T////////////////////AOAjXRkAAACYSURBVHjaLI8JDgMgCAQ5BVG3//9t0XYTE2Y5BPq0IGpwtxtTP4G5IFNMnmEKuCopPKUN8VTNpEylNgmCxjZa2c1kafpHSvMkX6sWe7PTkwRX1dY7gdyMRHZdZ98CF6NZT2ecMVaL9tmzTtMYcwbP y3XeTgZkF5s1OSHwRzo1fkILgWC5R0X4BHYu7t/136wO71DbvwVYADUkQegpokSjwAAAABJRU5ErkJggg=='.replace(' ', '+')
imgdata = base64.b64decode(data)
filename = 'some_image.jpg'  # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
        f.write(imgdata)

这篇关于保存base64图像python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆