Skip to content

Instantly share code, notes, and snippets.

@skvggor
Created May 7, 2014 10:41
Show Gist options
  • Save skvggor/1da5b034fced240a84ba to your computer and use it in GitHub Desktop.
Save skvggor/1da5b034fced240a84ba to your computer and use it in GitHub Desktop.
Convert PNG to data uri/base64
#!/usr/bin/python
# encoding: utf-8
import urllib
def convert_png_to_datauri(image_file):
image_encoded = urllib.quote(open(image_file, 'rb').read().encode('base64'))
return 'data:image/png;base64,' + image_encoded
if __name__ == '__main__':
print convert_png_to_datauri('/home/xxx/Copy/Photos/tecmundo.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment