Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 11th, 2012  |  syntax: None  |  size: 2.40 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import os
  2. import re
  3. from PIL import Image, ImageOps
  4. import shutil
  5. import contextlib
  6.  
  7. sixdigithex = re.compile('#([0-9A-Za-z]{3}(?:[0-9A-Za-z]{3})?)[^0-9A-Za-z]|$')
  8.  
  9. graypath = 'com.sencha.gxt.ui/src/main/java/com/sencha/gxt/theme/gray/'
  10. for dirpath, dirnames, filenames in os.walk(graypath):
  11.     print "In path %s" % dirpath
  12.     for filename in filenames:
  13.         print "  Examining file %s" % filename
  14.         root, ext = os.path.splitext(filename)
  15.         if ext in ('.gif', '.png', '.jpg'):
  16.             print "    Match! Converting..."
  17.             im = Image.open(os.path.join(dirpath, filename))
  18.             im = ImageOps.grayscale(im)
  19.             im.save(os.path.join(dirpath, filename))
  20.         if ext in ('.css', '.java'):
  21.             print "    Match! Converting..."
  22.             shutil.copyfile(os.path.join(dirpath, filename),
  23.                             os.path.join(dirpath, filename + '.old'))
  24.             with open(os.path.join(dirpath, filename + '.old')) as infile, open(os.path.join(dirpath, filename), 'w') as outfile:
  25.                 for line in infile:
  26.                     outbuilder = []
  27.                     outindex = 0
  28.                     for match in sixdigithex.finditer(line):
  29.                         print "      Found on line: %s" % line
  30.                         value = match.group(1)
  31.                         if not value: continue
  32.                         print "      Value is %s" % match.group(1)
  33.                         lenvalue = len(value)
  34.                         if lenvalue == 3:
  35.                             r = int(value[0] * 2, 16)
  36.                             g = int(value[1] * 2, 16)
  37.                             b = int(value[2] * 2, 16)
  38.                         elif lenvalue == 6:
  39.                             r = int(value[0:2], 16)
  40.                             g = int(value[2:4], 16)
  41.                             b = int(value[4:6], 16)
  42.                         else:
  43.                             raise Exception()
  44.                         l = r * 0.299 + g * 0.587 + b * 0.114
  45.                         hexcode = ('%0.2X' % l) * 3
  46.                         print "      Replacing %s with %s" % (match.group(1), hexcode)
  47.                         outbuilder.append(line[outindex:match.start(1)])
  48.                         outbuilder.append(hexcode)
  49.                         outindex += match.start(1) - outindex + lenvalue
  50.                     outbuilder.append(line[outindex:])
  51.                     outfile.write(''.join(outbuilder))