我想自动将带有png图像的html文件转换为单个html文件中包含的内联base64图像。有没有这样的随时可用的工具?
答案 0 :(得分:2)
这是我最近使用的简单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()