1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
import os.path
from PIL import Image
def find_directories(root = None):
if not root:
root = os.path.abspath(".")
test_directories = []
def accumulate_directories(arg, dirname, fnames):
arg.append(dirname)
os.path.walk(root, accumulate_directories , test_directories)
for d in test_directories: yield d
def run_resize(path):
print "Handling " , path
#Get current pwd
pwd = os.path.abspath (".")
#Go there
os.chdir (path)
#List of extensions
#Get files
for f in os.listdir ("."):
try:
print os.path.abspath (".")
im = Image.open(f)
print f, " opened"
width, height = im.size
size = width/4, height/4
im.thumbnail(size, Image.ANTIALIAS)
im.save(f)
except:
im = 0
#Come back
os.chdir (pwd)
def resize_all():
for path in find_directories():
yield run_resize, path
if __name__ == "__main__":
for call, elem in resize_all():
run_resize (elem) |
Partager