Guest User

python_css_b64

a guest
Mar 31st, 2011
664
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import re
  3.  
  4. dirname = os.path.dirname(__file__)
  5.  
  6. files = os.listdir(dirname)
  7. files_css = filter(lambda x: x.endswith(".css"), files)
  8.  
  9. for f in files_css:
  10.     css = open(f, "r").read()
  11.     matches = re.findall(r'url\(("(.*)")\)',  css)
  12.     for match in matches:
  13.         sprite_path = os.path.join(dirname, match[1])
  14.         sprite64 = open(sprite_path, "rb").read().encode("base64")[:-1]
  15.         css = css.replace(match[0], "data:image/png;base64," + sprite64)
  16.         css = css.replace("\n", "")
  17.     css64_file = open(f + "64", "w")
  18.     css64_file.write(css)
  19.     css64_file.close()
RAW Paste Data