-3

I'd like to automatically convert html files with png images to inline base64 images included in single html file. Is there any such ready-to-use tool?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
nosohroch
  • 21
  • 2
  • A java programmer could use [this](http://stackoverflow.com/questions/9388264/jeditorpane-with-inline-image) to make a tool. – Joop Eggen Jul 03 '13 at 14:11

1 Answers1

3

Here is simple python script which I used recently... It act as a filter:

#!/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()
Jiří Polcar
  • 1,192
  • 9
  • 15