Skip to content

Instantly share code, notes, and snippets.

@zet4
Forked from bergantine/gist:1171682
Created September 23, 2015 06:41
Show Gist options
  • Save zet4/5a333d34a7624f5e3846 to your computer and use it in GitHub Desktop.
Save zet4/5a333d34a7624f5e3846 to your computer and use it in GitHub Desktop.
Python Image Encoding in Data URI Scheme Base 64. #python #base64
data_uri = open("sample.png", "rb").read().encode("base64").replace("\n", "")
# HTML Image Element
img_tag = '<img alt="" src="data:image/png;base64,{0}">'.format(data_uri)
print img_tag
# CSS Background Image
css = 'background-image: url(data:image/png;base64,{0});'.format(data_uri)
print css
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment