python保存base64编码的图片,保存base64图像python

本文讨论了如何在Python中正确保存Base64编码的图片。作者遇到了因错误添加'data:image/png;base64,'前缀和空格替换导致的错误。解决方案是直接对Base64字符串进行解码,省略不必要的前缀,并用'+'替换空格,然后成功写入文件。" 80395198,7375151,手动编译与安装Linux内核详解,"['操作系统', 'Linux']
摘要由CSDN通过智能技术生成

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

9667e0298b0a821872e37b8997f0f72e.png

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(" ", "+")

And then I do this

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)

But this causes this error

Traceback (most recent call last):

File "/var/www/cgi-bin/save_info.py&

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值