'''MakeReductions.py Copy a folder tree containing .JPG image files, making reduced-resolution copies of the images found, and naming the new folders slightly differently. SF is the scale factor for reduction. The default given is: SF = 0.1 ''' SF = 0.1 # scale factor RF = 1.0/SF # resampling factor import os # operating system functions def list_JPGs(folder): files = os.listdir(folder) return filter(lambda f: f[-4:]==".JPG", files) def list_folders(folder): files = os.listdir(folder) return filter(\ lambda f: os.path.isdir(os.path.join(folder,f)), files) def reduce_image(source_path, destination_path): print "Preparing to reduce the image: "+\ source_path+" to get: "+destination_path pmOpenImage(1, source_path) width = pmGetImageWidth(1) height = pmGetImageHeight(1) w1 = int(SF*width) h1 = int(SF*height) pmSetSource1(1) newWin = pmNewImage(2, destination_path, w1, h1, 255, 0, 0) pmSetDestination(newWin) pmSetFormula("S1("+str(RF)+"*x,"+str(RF)+"*y)") pmCompute() pmSaveImageAs(newWin, destination_path) def reduce_all_in_folder_tree(source_path, source_folder_name, destination_path): # create destination folder. new_destination_path =\ os.path.join(destination_path, source_folder_name+"_reduced_by_"+str(SF)) print "new_destination_path = "+new_destination_path os.makedirs(new_destination_path) # go into the source folder and reduce all the JPGs there. new_source_path = os.path.join(source_path, source_folder_name) print "new_source_path = "+new_source_path JPGs = list_JPGs(new_source_path) for fn in JPGs: new_fn = fn[:-4]+"_reduced_by_"+str(SF)+".JPG" reduce_image(os.path.join(new_source_path, fn), os.path.join(new_destination_path, new_fn)) # Process subdirectories recursively: print "Requesting list of folders in "+new_source_path folders = list_folders(new_source_path) print "Folders to be processed: "+str(folders) for f in folders: # Now here is the recursive call, made once per subfolder: reduce_all_in_folder_tree(new_source_path, f, new_destination_path) # Now let's initiate the whole operation: reduce_all_in_folder_tree('.', 'My-Images', '.')