Skip to content

Instantly share code, notes, and snippets.

@josephsu
Created January 8, 2013 04:39
Show Gist options
  • Save josephsu/4481205 to your computer and use it in GitHub Desktop.
Save josephsu/4481205 to your computer and use it in GitHub Desktop.
decode Data URI to mime and data parts
import base64
def decodeDataUri(uristr):
comma = uristr.find(',')
urihead = uristr.find('data:')
if urihead is None or comma is None:
return None
headpart = uristr[urihead + 5:comma]
firstsep = headpart.find(';')
if firstsep is None:
firstsep = len(firstsep)
mimetype = headpart[0:firstsep]
b64data = uristr[(comma + 1):]
rawdata = base64.b64decode(b64data)
return ( mimetype, rawdata )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment