import os import constants import numpy as np from scipy import misc, ndimage def resize(image, dim1, dim2): return misc.imresize(image, (dim1, dim2)) def fileWalk(directory, destPath): try: os.makedirs(destPath) except OSError: if not os.path.isdir(destPath): raise for subdir, dirs, files in os.walk(directory): for file in files: if len(file) <= 4 or file[-4:] != '.jpg': continue pic = misc.imread(os.path.join(subdir, file)) dim1 = len(pic) dim2 = len(pic[0]) if dim1 > dim2: pic = np.rot90(pic) picResized = resize(pic,constants.DIM1, constants.DIM2) misc.imsave(os.path.join(destPath, file), picResized) def main(): prepath = os.path.join(os.getcwd(), 'Combined') batteryDir = os.path.join(prepath, 'battery') biologicalDir = os.path.join(prepath, 'biological') brownglassDir = os.path.join(prepath, 'brown-glass') cardboardDir = os.path.join(prepath, 'cardboard') clothesDir = os.path.join(prepath, 'clothes') greenglassDir = os.path.join(prepath, 'green-glass') metalDir = os.path.join(prepath, 'metal') paperDir = os.path.join(prepath, 'paper') plasticDir = os.path.join(prepath, 'plastic') shoesDir = os.path.join(prepath, 'shoes') trashDir = os.path.join(prepath, 'trash') whiteglassDir = os.path.join(prepath, 'white-glass') destPath = os.path.join(os.getcwd(), 'Combined-resized') try: os.makedirs(destPath) except OSError: if not os.path.isdir(destPath): raise #BATTERY fileWalk(batteryDir, os.path.join(destPath, 'battery')) #BIOLOGICAL fileWalk(biologicalDir , os.path.join(destPath, 'biological')) #BROWN-GLASS fileWalk(brownglassDir, os.path.join(destPath, 'brown-glass')) #CARDBOARD fileWalk(cardboardDir, os.path.join(destPath, 'cardboard')) #CLOTHES fileWalk(clothesDir, os.path.join(destPath, 'clothes')) #GREEN-GLASS fileWalk(greenglassDir, os.path.join(destPath, 'green-glass')) #METAL fileWalk(metalDir, os.path.join(destPath, 'metal')) #PAPER fileWalk(paperDir, os.path.join(destPath, 'paper')) #PLASTIC fileWalk(plasticDir, os.path.join(destPath, 'plastic')) #SHOES fileWalk(shoesDir, os.path.join(destPath, 'shoes')) #TRASH fileWalk(trashDir, os.path.join(destPath, 'trash')) #WHITE-GLASS fileWalk(whiteglassDir, os.path.join(destPath, 'white-glass')) if __name__ == '__main__': main()