我最近在网上找到一个文件,我确实需要它的原始url,但它被编码成Base64。这是一个图像。
URL 以这样的内容开头:
data:image/png;base64,
然后是大量的数字和字母。
我的问题是,如何将其解码为原始形式?例如而不是
mwo1fw#
到http://etc
使用此网络实用程序:
http://www.motobit.com/util/base64-decoder-encoder.asp
设置输出格式为二进制,然后复制粘贴
data:image/png;base64,
后面的base64数据;您的浏览器将下载该文件。将它重命名为 PNG 就可以了。
保存图像的另一种方法是复制粘贴
data:image/png;base64,
,然后是浏览器 url 字段中的字符..试试这个在线 base64 解码/编码工具 http://base64online.org/decode/
只需将您的 base64 编码图像粘贴到此处,它就会显示图像和下载链接。
在 Linux 机器上,您可以使用这个
base64
实用程序。只需两步。
data:image/png;base64,
base64 -d image.base64 > img.jpg
就是这样。
img.jpg
是您的图像文件。通过从文件查看器中双击文件来查看它。
你可以在这里解码它:http://base64decode.net/ 并将响应保存为 .png 文件
如果您有兼容 Html5 的浏览器并启用了 Javascript,您可以使用 Base 64 Online
确保从编码的 base64 图像中删除
data:image/png;base64,
。
然后将字符串粘贴到文本输入区域并单击“解码>下载”并将结果保存为.png
如何导出 Salesforce 报价 PDF 文件并使用 Linux 命令行读取将其从 Base64 转换回 PDF
2. 使用文本编辑器、TextPad 打开csv 文件。不要使用记事本,因为它无法处理大数据并将其截断。 3. 当您打开文件以及复制/粘贴数据时,TextPad 可以处理大数据并尊重任何换行符等。 4. 转到数据中的特定行,选择并复制包含 PDF Base64 编码数据的单元格字段字符串。确保你一直选择它到最后 - 但不要在它后面包含后续字段。
作为记录,这是一个快速而肮脏的 Python 3 片段。
import base64
def decode_base64_url(url):
assert url.startswith('data:image/')
assert ';base64,' in url
schema, payload = url.split(',', 1)
# Maybe parse the schema if you want to know the image's type
# type = schema.split('/', 1)[-1].split(';', 1)[0]
return base64.urlsafe_b64decode(payload)
您可能希望从一个函数中运行它
open(..., 'wb')
将有效负载写入二进制文件,或者以其他方式将其传递给可以处理原始图像 bytes
的东西。也许另请参阅Convert string in base64 to image and save on filesystem in Python
您可以使用这个网站: https://ganixo.site
提交你的代码,他会生成一个链接供你下载解码后的图片。
这里是对this excellent answer的详细说明,使其成为完全命令行:
# optionally make a test image
echo "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII" > test.png
# chop off the leading MIME, decode it and redirect to a file
cat test.png | cut -c23- | base64 -d > decoded.png
# use whatever image-viewing application you want to see the result
firefox decoded.png
或者使用 NodeJS:
const fs = require("node:fs/promises");
(async () => {
const b64 = await fs.readFile("test.png", "ascii");
await fs.writeFile("decoded.png", b64.split(",")[1], "base64");
})();