@register.filter
def data_uri(thumbnail):
 try:
  thumbnail.open('rb')
  data = thumbnail.read()
 finally:
  thumbnail.close()
 mime_type = (mimetypes.guess_type(str(thumbnail.file))[0] or 'application/octet-stream')
 data = b64encode(data).decode('utf-8')
 return 'data:{0};base64,{1}'.format(mime_type, data)

@ register . filter def data_uri ( thumbnail ) : try : thumbnail . open ( 'rb' ) data = thumbnail . read ( ) finally : thumbnail . close ( ) mime_type = ( mimetypes . guess_type ( str ( thumbnail . file ) ) [ 0 ] or 'application/octet-stream' ) data = b64encode ( data ) . decode ( 'utf-8' ) return 'data:{0};base64,{1}' . format ( mime_type , data )
this filter will return the base64 encoded data uri for a given thumbnail object .