- import os
- import re
- from PIL import Image, ImageOps
- import shutil
- import contextlib
- sixdigithex = re.compile('#([0-9A-Za-z]{3}(?:[0-9A-Za-z]{3})?)[^0-9A-Za-z]|$')
- graypath = 'com.sencha.gxt.ui/src/main/java/com/sencha/gxt/theme/gray/'
- for dirpath, dirnames, filenames in os.walk(graypath):
- print "In path %s" % dirpath
- for filename in filenames:
- print " Examining file %s" % filename
- root, ext = os.path.splitext(filename)
- if ext in ('.gif', '.png', '.jpg'):
- print " Match! Converting..."
- im = Image.open(os.path.join(dirpath, filename))
- im = ImageOps.grayscale(im)
- im.save(os.path.join(dirpath, filename))
- if ext in ('.css', '.java'):
- print " Match! Converting..."
- shutil.copyfile(os.path.join(dirpath, filename),
- os.path.join(dirpath, filename + '.old'))
- with open(os.path.join(dirpath, filename + '.old')) as infile, open(os.path.join(dirpath, filename), 'w') as outfile:
- for line in infile:
- outbuilder = []
- outindex = 0
- for match in sixdigithex.finditer(line):
- print " Found on line: %s" % line
- value = match.group(1)
- if not value: continue
- print " Value is %s" % match.group(1)
- lenvalue = len(value)
- if lenvalue == 3:
- r = int(value[0] * 2, 16)
- g = int(value[1] * 2, 16)
- b = int(value[2] * 2, 16)
- elif lenvalue == 6:
- r = int(value[0:2], 16)
- g = int(value[2:4], 16)
- b = int(value[4:6], 16)
- else:
- raise Exception()
- l = r * 0.299 + g * 0.587 + b * 0.114
- hexcode = ('%0.2X' % l) * 3
- print " Replacing %s with %s" % (match.group(1), hexcode)
- outbuilder.append(line[outindex:match.start(1)])
- outbuilder.append(hexcode)
- outindex += match.start(1) - outindex + lenvalue
- outbuilder.append(line[outindex:])
- outfile.write(''.join(outbuilder))