!/usr/bin/python
import os
import cPickle as p
import re
import Image
def imageCompre(imagedir = '.', imagelistfile="image.data"):
datafile = imagedir+'/'+imagelistfile
allowImageType = ['jpg','jpeg','gif','png']
pre = re.compile('^big|^sml|^mid*');
if os.path.isfile(datafile):
f = file(datafile)
imagelist = p.load(f)
f.close()
else:
imagelist = []
for item in os.listdir(imagedir):
if item:
if os.path.isdir(imagedir+"/"+item):
imageCompre(imagedir+"/"+item,'image.data')
else:
if item.split('.')[-1] in allowImageType and item not in imagelist and not pre.match(item):
imagelist.append(item);
#convert image file
convertImage(imagedir,item)
#update image.data
if imagelist:
f = file(datafile, 'w')
p.dump(imagelist, f)
f.close()
#delete imagelist tuple
del imagelist
def convertImage(imagedir,item):
sourcefile = imagedir+"/"+item
im = Image.open(sourcefile);
if im.size[0]<=120:
os.system("cp %s %s/big-%s -a" %(sourcefile,imagedir,item))
os.system("cp %s