# -*- coding: UTF-8 -*-
from django import template
from base64 import b64encode
register = template.Library()
@register.filter
def dataURI(filename, mime = None):
"""
This filter will return data URI for given file, for more info go to:
http://en.wikipedia.org/wiki/Data_URI_scheme
Sample Usage:
will be filtered into:
"""
with open(filename, "rb") as file:
data = file.read()
encoded = b64encode(data)
mime = mime + ";" if mime else ";"
return "data:%sbase64,%s" % (mime, encoded)