import os import PIL from PIL import Image SHOW_FILES = False # set to True if you want to see them after creation. MAX_X = 560 output_format = 'PNG' path = '/home/zeffii/Desktop/AUTO_RESIZE/gl_fillet/' os.chdir(path) # set this folder active mode = 'AA' # ('AA', 'NOAA') # warning: ensure no non images or folders in folder that contains your # larger images. filelist = os.listdir(path) # make a 'mod' directory mod_path = path+"mod" os.mkdir(mod_path) for file in filelist: filepath = path+file # get name, assume filename is 3 chars long. make new name with "_mod" img_name = file[:-4]+"_mod" # open the file, get dimensions db = Image.open(filepath) img_x, img_y = db.size # resize according to a maximum x width NEW_Y = (MAX_X*img_y)/img_x if img_x <= MAX_X: # if smaller or equal to 560, save file unchanged continue else: # scale the image down to have x width of MAX_X if mode == 'AA': mod_image = db.resize((MAX_X, NEW_Y), Image.ANTIALIAS) if mode == 'NOAA': mod_image = db.resize((MAX_X, NEW_Y)) mod_image.save(mod_path+"/"+img_name+".png", format=output_format) if SHOW_FILES: mod_image.show()