简体   繁体   English

无法将 base64 字符串转换为图像

[英]Cannot convert base64 string into image

Can someone help me turn this base64 data into image?有人可以帮我把这个 base64 数据转换成图像吗? I don't know if it's because the data was not decoded properly or anything else.我不知道是因为数据没有正确解码还是其他原因。 Here is how I decoded the data:这是我解码数据的方式:

import base64

c_data = { the data in the link (string type) }

c_decoded = base64.b64decode(c_data)

But it gave the error Incorrect Padding so I followed some tutorials and tried different ways to decode the data.但它给出了错误填充不正确所以我按照一些教程尝试了不同的方法来解码数据。

c_decoded = base64.b64decode(c_data + '=' * (-len(c_data) % 4))

c_decoded = base64.b64decode(c_data + '=' * ((4 - len(c_data) % 4) % 4)

Both ways decoded the data without giving the error Incorrect Padding but now I can't turn the decoded data into image.两种方式都解码了数据而没有给出错误 Incorrect Padding 但现在我无法将解码数据转换为图像。
I have tried creating an empty png then write the decoded data into it:我尝试创建一个空的 png,然后将解码后的数据写入其中:

from PIL import Image

with open('c.png', 'wb') as f:
    f.write(c_decoded)
image = Image.open('c.png')
image.show()

It didn't work and gave the error: cannot identify image file 'c.png'它没有工作并给出了错误:无法识别图像文件“c.png”
I have tried using BytesIO:我试过使用 BytesIO:

from PIL import Image
import io
from io import BytesIO

image = Image.open(io.BytesIO(c_decoded))
image.show()

Now it gave the error: cannot identify image file <_io.BytesIO object at 0x0000024082B20270>现在它给出了错误:无法识别图像文件 <_io.BytesIO object at 0x0000024082B20270>
Please help me.请帮我。

Not sure if you definitely need a Python solution, or you just want help decoding your image like the first line of your question says, and you thought Python might be needed.不确定您是否确实需要 Python 解决方案,或者您只是想像问题的第一行所说的那样帮助解码图像,并且您认为可能需要 Python。

If the latter, you can just use ImageMagick in the Terminal:如果是后者,您可以在终端中使用ImageMagick

cat YOURFILE.TXT | magick inline:- result.png

在此处输入图像描述

Or equivalently and avoiding "Useless Use of cat" :或者等价地避免“猫的无用使用”

magick inline:- result.png < YOURFILE.TXT

If the former, you can use something like this (untested):如果是前者,你可以使用这样的东西(未经测试):

from urllib import request

with request.urlopen('data:image/png;base64,iVBORw0...')  as response:
   im = response.read()

Now im contains a PNG-encoded [^1] image, so you can either save to disk as such:现在im包含一个 PNG 编码的 [^1] 图像,因此您可以这样保存到磁盘:

with open('result.png','wb') as f:
    f.write(im)

Or, you can wrap it in a BytesIO and open into a PIL Image :或者,您可以将其包装在BytesIO中并打开为PIL Image

from io import BytesIO
from PIL import Image

pilImage = Image.open(BytesIO(im))

[^1]: Note that I have blindly assumed it is a PNG, it may be JPEG, so you should ideally look at the start of the DataURI to determine a suitable extension for saving your file. [^1]:请注意,我盲目地假设它是 PNG,也可能是 JPEG,因此您最好查看 DataURI 的开头以确定用于保存文件的合适扩展名。

Credit to @jps for explaining why my code didn't work.感谢@jps 解释了为什么我的代码不起作用。 Check out @Mark Setchell solution for the reliable way of decoding base64 data (his code fixes my mistake that @jps pointed out)查看@Mark Setchell 解决方案,了解解码 base64 数据的可靠方法(他的代码修复了@jps 指出的我的错误)

So basically remove the [data:image/png;base64,] at the beginning of the base64 string because it is just the format of the data.所以基本上去掉base64字符串开头的[data:image/png;base64,]因为它只是数据的格式。 Change this:改变这个:

c = "data:image/png;base64,iVBORw0KGgoAAAANSUh..."

to this:对此:

c = "iVBORw0KGgoAAAANSUh..."

and now we can use现在我们可以使用

c_decoded = base64.b64decode(c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM