|
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 |
|
|
|
|
|
|
|
fileWalk(batteryDir, os.path.join(destPath, 'battery')) |
|
|
|
|
|
fileWalk(biologicalDir , os.path.join(destPath, 'biological')) |
|
|
|
|
|
fileWalk(brownglassDir, os.path.join(destPath, 'brown-glass')) |
|
|
|
|
|
fileWalk(cardboardDir, os.path.join(destPath, 'cardboard')) |
|
|
|
|
|
fileWalk(clothesDir, os.path.join(destPath, 'clothes')) |
|
|
|
|
|
fileWalk(greenglassDir, os.path.join(destPath, 'green-glass')) |
|
|
|
|
|
fileWalk(metalDir, os.path.join(destPath, 'metal')) |
|
|
|
|
|
fileWalk(paperDir, os.path.join(destPath, 'paper')) |
|
|
|
|
|
fileWalk(plasticDir, os.path.join(destPath, 'plastic')) |
|
|
|
|
|
fileWalk(shoesDir, os.path.join(destPath, 'shoes')) |
|
|
|
|
|
fileWalk(trashDir, os.path.join(destPath, 'trash')) |
|
|
|
|
|
fileWalk(whiteglassDir, os.path.join(destPath, 'white-glass')) |
|
|
|
if __name__ == '__main__': |
|
main() |