Skip to content

Instantly share code, notes, and snippets.

@ale-rt
Created May 14, 2015 16: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 ale-rt/73b21d1bde590856e4e9 to your computer and use it in GitHub Desktop.
Save ale-rt/73b21d1bde590856e4e9 to your computer and use it in GitHub Desktop.
Script that creates css rules to add background png images with data-uris
#!/usr/bin/env python
from PIL import Image
from base64 import encodestring
import sys
template = '''.%(classname)s {
width: %(width)spx;
height: %(height)spx;
background-repeat: no-repeat;
background-image: url(data:image/png;base64,%(data)s);
}
'''
for f in sys.argv[1:]:
width, height = Image.open(f).size
img_data = open(f).read()
data = {
'classname': f.partition('.')[0],
'data': ''.join(encodestring(img_data).split()),
'width': width,
'height': height,
}
print template % data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment