gpt4 book ai didi

python - 将base64转换为图像: incorrect padding error

转载 作者:行者123 更新时间:2023-12-01 04:30:42 29 4
gpt4 key购买 nike

我从 base64 格式的表单中收到一张图像,我想将其转换回 PNG 文件类型。

为此我尝试了以下方法

attempt1 = base64.b64decode(picture.data)
attempt2 = picture.data.decode('base64')

但我收到错误错误:填充不正确

我看到了this answer并尝试使用该函数,但是我得到了相同的错误。

我可以做什么来解决这个问题?谢谢。

编辑

我的 Base64 图像如下所示(字符列表更长):

data:image/png;base64, OouhoGUiyf+YdiHl==

最佳答案

看来你的数据是DataURL方案

Data URLs are composed of four parts: a prefix (data:), a MIME type indicating the type of data, an optional base64 token if non-textual, and the data itself:

data:[<mediatype>][;base64],<data>

您应该仅提取 <data>字符串的一部分:

base64.decodebytes(picture.data.split(",")[1])

关于python - 将base64转换为图像: incorrect padding error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428950/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com