您如何对 PNG 图像进行 base-64 编码以在 CSS 文件中的 data-uri 中使用?

2022-01-21 00:00:00 python image base64 data-uri css

我想对 PNG 文件进行 base-64 编码,以将其包含在我的样式表中的 data:url 中.我该怎么做?

I want to base-64 encode a PNG file, to include it in a data:url in my stylesheet. How can I do that?

我在 Mac 上,所以 Unix 命令行上的东西会很好用.基于 Python 的解决方案也很棒.

I’m on a Mac, so something on the Unix command line would work great. A Python-based solution would also be grand.

推荐答案

这应该在 Python 中完成:

This should do it in Python:

import base64
encoded = base64.b64encode(open("filename.png", "rb").read())

相关文章