Last active
March 16, 2025 14:57
-
-
Save MalekKamel/16a46f6d299503e1b004ce93a8494feb to your computer and use it in GitHub Desktop.
Python script that you can run in the terminal to resize images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from PIL import Image | |
| def resize_images_in_folder(folder_path, output_folder, size=(800, 600)): | |
| """ | |
| Resizes all images in the specified folder and saves them to the output folder. | |
| :param folder_path: Path to the folder containing the images. | |
| :param output_folder: Path to the folder where resized images will be saved. | |
| :param size: Tuple (width, height) for the new size. Default is (800, 600). | |
| """ | |
| # Create the output folder if it doesn't exist | |
| if not os.path.exists(output_folder): | |
| os.makedirs(output_folder) | |
| # Loop through all files in the folder | |
| for filename in os.listdir(folder_path): | |
| file_path = os.path.join(folder_path, filename) | |
| # Check if the file is an image | |
| if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')): | |
| try: | |
| with Image.open(file_path) as img: | |
| # Resize the image using LANCZOS resampling | |
| resized_img = img.resize(size, Image.Resampling.LANCZOS) | |
| # Save the resized image to the output folder | |
| output_path = os.path.join(output_folder, filename) | |
| resized_img.save(output_path) | |
| print(f"Resized and saved {filename} to {output_path}") | |
| except Exception as e: | |
| print(f"Failed to process {filename}: {e}") | |
| else: | |
| print(f"Skipping non-image file: {filename}") | |
| if __name__ == "__main__": | |
| import argparse | |
| # Set up argument parsing | |
| parser = argparse.ArgumentParser(description="Resize images in a folder.") | |
| parser.add_argument("folder_path", help="Path to the folder containing images") | |
| parser.add_argument("output_folder", help="Path to the folder to save resized images") | |
| parser.add_argument("--width", type=int, default=800, help="Width of the resized images") | |
| parser.add_argument("--height", type=int, default=600, help="Height of the resized images") | |
| args = parser.parse_args() | |
| # Resize images | |
| resize_images_in_folder(args.folder_path, args.output_folder, (args.width, args.height)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script uses the
Pillowlibrary (PIL) to handle image resizing.Prerequisites:
python3 --versionin the terminal.pip3 install Pillow.How to Run the Script:
Save the Script: Save the script as
resize_images.py.Open Terminal: Open the terminal on your macOS.
Navigate to the Script's Directory: Use the
cdcommand to navigate to the directory where you saved the script.cd /path/to/your/scriptInstall Pillow:
Open your terminal and run the following command to install the
Pillowlibrary:Run the Script: Run the script using Python 3, specifying the folder containing the images and the output folder where resized images will be saved.
/path/to/input/folderwith the path to the folder containing the images you want to resize./path/to/output/folderwith the path to the folder where you want to save the resized images.--widthand--heightparameters to your desired dimensions.Example:
This command will resize all images in the
~/Pictures/Originalfolder to 1024x768 pixels and save them in the~/Pictures/Resizedfolder.Notes: