Skip to content

Instantly share code, notes, and snippets.

@MalekKamel
Last active March 16, 2025 14:57
Show Gist options
  • Select an option

  • Save MalekKamel/16a46f6d299503e1b004ce93a8494feb to your computer and use it in GitHub Desktop.

Select an option

Save MalekKamel/16a46f6d299503e1b004ce93a8494feb to your computer and use it in GitHub Desktop.
Python script that you can run in the terminal to resize images
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))
@MalekKamel
Copy link
Copy Markdown
Author

MalekKamel commented Mar 16, 2025

This script uses the Pillow library (PIL) to handle image resizing.

Prerequisites:

  1. Python: Ensure Python is installed on your macOS. You can check by running python3 --version in the terminal.
  2. Pillow Library: Install the Pillow library by running pip3 install Pillow.

How to Run the Script:

  1. Save the Script: Save the script as resize_images.py.

  2. Open Terminal: Open the terminal on your macOS.

  3. Navigate to the Script's Directory: Use the cd command to navigate to the directory where you saved the script.

    cd /path/to/your/script
  4. Install Pillow:
    Open your terminal and run the following command to install the Pillow library:

    pip3 install Pillow
  5. 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.

    python3 resize_images.py /path/to/input/folder /path/to/output/folder --width 800 --height 600
    • Replace /path/to/input/folder with the path to the folder containing the images you want to resize.
    • Replace /path/to/output/folder with the path to the folder where you want to save the resized images.
    • You can adjust the --width and --height parameters to your desired dimensions.

Example:

python3 resize_images.py ~/Pictures/Original ~/Pictures/Resized --width 1024 --height 768

This command will resize all images in the ~/Pictures/Original folder to 1024x768 pixels and save them in the ~/Pictures/Resized folder.

Notes:

  • The script will skip non-image files.
  • The resized images will maintain their original file names but will be saved in the specified output folder.
  • If the output folder does not exist, the script will create it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment