from PIL import Image import os class BatchResizeParams: InputPath = "Input" OutputPath = "Output" RescaleFactor = 1 class BatchResize: @staticmethod def ResizeFolder(p_BatchResizeParams: BatchResizeParams): print("Starting Rescale...") print("Scanning Folder : "+p_BatchResizeParams.InputPath) folder = BatchResize.ScanInputFolder(p_BatchResizeParams) print(folder) resizedImages: list = [] for file in folder: print("Rescale Image : " + file) resizedImages.append(BatchResize.ResizeFile(p_BatchResizeParams, file)) print("Done.") i:int = 0 for image in resizedImages: print("Saving Image : " + folder[i]) BatchResize.SaveFile(p_BatchResizeParams, folder[i], image) print("Done.") i+=1 @staticmethod def SaveFile(p_BatchResizeParams: BatchResizeParams, p_currentFilePath: str, p_image: Image.Image): path = os.path.join(p_BatchResizeParams.OutputPath, os.path.basename(p_currentFilePath)) p_image.save(path) @staticmethod def ResizeFile(p_BatchResizeParams: BatchResizeParams, p_currentFilePath: str) -> Image.Image: image = BatchResize.LoadFile(p_currentFilePath) fileres = image.size x_size = int(fileres[0] * p_BatchResizeParams.RescaleFactor) y_size = int(fileres[1] * p_BatchResizeParams.RescaleFactor) newImage = image.resize([x_size, y_size]) return newImage @staticmethod def ScanInputFolder(p_BatchResizeParams: BatchResizeParams): folder = [] l_dir = os.listdir(p_BatchResizeParams.InputPath) for file in l_dir: if file.endswith((".png",".jpg",".jpeg")): folder.append(os.path.join(p_BatchResizeParams.InputPath, file)) return folder @staticmethod def LoadFile(p_currentFilePath: str) -> Image.Image: return Image.open(p_currentFilePath) param: BatchResizeParams = BatchResizeParams() param.RescaleFactor = 0.6 print("Rescale Toolkit") BatchResize.ResizeFolder(param)