【问题标题】:Convert html images to inline base64 [closed]将 html 图像转换为内联 base64 [关闭]
【发布时间】:2013-07-01 06:36:11
【问题描述】:

我想自动将带有 png 图像的 html 文件转换为包含在单个 html 文件中的内联 base64 图像。有没有这样的即用型工具?

【问题讨论】:

  • Java 程序员可以使用this 制作工具。

标签: html


【解决方案1】:

这是我最近使用的简单python脚本...它充当过滤器:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import fileinput
import re
import base64
import mimetypes
import os


def replacement(match):

    fn = match.groups()[0]

    if os.path.isfile(fn):
        return 'src="data:%s;base64,%s"' % (mimetypes.guess_type(fn)[0], base64.b64encode(open(fn, 'rb').read()))

    return match.group()



def main():

    fi = fileinput.FileInput(openhook=fileinput.hook_encoded("utf8"))

    while True:
        line = fi.readline()
        if not line:
            break
        print re.sub(r'src="(.*?)"', replacement, line).encode('utf-8'),



if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 2017-09-12
    相关资源
    最近更新 更多