Skip to content

Instantly share code, notes, and snippets.

@Matjaz-B
Created October 3, 2017 11:04
Show Gist options
  • Save Matjaz-B/8980e576e376e703fa7bcac50ed7f91e to your computer and use it in GitHub Desktop.
Save Matjaz-B/8980e576e376e703fa7bcac50ed7f91e to your computer and use it in GitHub Desktop.
Embed images (data URI) into HTML document
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Embed images into HTML document
"""
import base64
import mimetypes
import os
import re
import io
import sys
## from https://gist.github.com/jsocol/1089733
class FileNotFoundError(Exception):
pass
def img_to_data(path):
"""Convert a file (specified by a path) into a data URI."""
if not os.path.exists(path):
raise FileNotFoundError
mime, _ = mimetypes.guess_type(path)
with open(path, 'rb') as fp:
data = fp.read()
data64 = u''.join(base64.encodestring(data).splitlines())
return u'data:%s;base64,%s' % (mime, data64)
def usage(argv):
print('Embed images into HTML document')
print('Output file is postfixed _embedded')
print('Usage: {:s} <path-to-file>'.format(argv[0]))
if __name__ == '__main__':
try:
infile = sys.argv[1]
except IndexError:
usage(sys.argv)
sys.exit(1)
outfile = re.sub(r'(.+)\.(.+)$', r'\1_embedded.\2', infile);
print("Reading: {:s}\nSaving to: {:s}".format(infile, outfile))
fin = io.open(infile, 'r', encoding="utf-8")
fout = io.open(outfile, 'w', encoding="utf-8")
din = fin.read()
fin.close()
found = re.findall("<img[^>]*src=\"([^\"]+)", din)
for img in found:
try:
uri = img_to_data(img)
except FileNotFoundError:
print("File {:s} is not found".format(img))
sys.exit(2)
print ("Replace {:s} with {:s}...".format(img, uri[0:50]))
din = din.replace(img, uri)
fout.write(din)
fout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment