Skip to content

Instantly share code, notes, and snippets.

@LouisGJBertrand
Created May 25, 2024 06:27
Show Gist options
  • Save LouisGJBertrand/b26032c8292ac6d513ef9c6df9e4afd2 to your computer and use it in GitHub Desktop.
Save LouisGJBertrand/b26032c8292ac6d513ef9c6df9e4afd2 to your computer and use it in GitHub Desktop.
Batch Resize Images
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)
Simply download or copy the file in a folder
create Two directories aside from the file:
- Input
- Output
Copy all the images you want to resize in Input
Execute the script
This script requires Python Pillow Library to work
https://python-pillow.org/
You can change the resize factor (1 = Unchanged) in param.RescaleFactor line 66
Feel free to make it a console argument
This file contains Two Classes:
BatchResizeParams : The parametters of the tool
BatchResize : The class containing the static methods for the tool
You can reuse this script everywhere you want.
All you have to do is declaring the params (you can define custom input and output paths both relative and/or absolute + the rescale factor)
and execute BatchResize.ResizeFolder(param) with param as your BatchResizeParams instance
To add more file formats than PNG and JPEG, add every file suffix you need in the array line 55.
This line tests if the current file being scanned contains one of the suffix in the array
Some of the code come from internet but the structure had been written exclusively for this tool as well as few methods.
Sources:
https://cloudinary.com/guides/bulk-image-resize/python-image-resize-with-pillow-and-opencv
https://www.geeksforgeeks.org/python-pil-image-resize-method/
https://www.geeksforgeeks.org/python-pil-image-save-method/
https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment