Skip to content

Instantly share code, notes, and snippets.

@GeorgeNava
Created May 8, 2010 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgeNava/394588 to your computer and use it in GitHub Desktop.
Save GeorgeNava/394588 to your computer and use it in GitHub Desktop.
Converts all images in a folder to base64
'''
This program converts all images in a folder to base64
for embedding in an html page inside a <style> tag like:
<style>
.img_name1{content:url("data:image/jpg;base64,abcdef...");}
.img_name2{content:url("data:image/jpg;base64,abcdef...");}
</style>
The 'content' property only works in webkit browsers.
'''
import os,glob,base64
path = './img/'
name = 'base64.css'
def save64(path,ext):
files = glob.glob(os.path.join(path,'*.%s'%ext))
for img in files:
print img
fh = open(img,"rb")
fc = fh.read()
fh.close()
fb = base64.b64encode(fc)
fn = os.path.splitext(os.path.basename(img))[0]
out.write('.img_')
out.write(fn)
out.write('{content:url("data:image/%s;base64,'%ext)
out.write(fb)
out.write('");}\n')
out = open(os.path.join(path,name),'w')
out.write('<style>\n')
save64(path,'jpg')
save64(path,'png')
save64(path,'gif')
out.write('</style>\n')
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment