This Python script compresses images in a specified folder, reducing their file size from megabytes (MB) to kilobytes (KB) while maintaining acceptable quality. It uses the Pillow library to resize and compress images, saving the results in a new subfolder.
- Supports common image formats: JPG, JPEG, PNG, BMP, GIF.
- Resizes images to a maximum resolution of 1024x1024 pixels (configurable).
- Compresses images to JPEG format with adjustable quality settings.
- Preserves original images and saves compressed versions in a
compressed
subfolder. - Displays file size reduction for each processed image.
- Python: Version 3.6 or higher.
- Pillow: Python Imaging Library (PIL) fork for image processing.
- Operating System: Windows, macOS, or Linux.
- Hardware: Any modern CPU (e.g., Intel i3, AMD Ryzen 3) with 4GB+ RAM recommended for large image batches.
Ensure Python 3.6 or higher is installed. Download from python.org if needed. Verify installation:
python --version
A virtual environment is recommended to manage dependencies:
- Navigate to your project directory:
mkdir image_compression cd image_compression
- Create a virtual environment named
venv
:python -m venv venv
- Activate the virtual environment:
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
With the virtual environment activated, install Pillow:
pip install Pillow
Verify installation:
pip show Pillow
Save the compress_images.py
script (provided separately) in your project directory.
-
Prepare a folder containing images (e.g., JPG, PNG).
-
Run the script with the folder path as an argument:
python compress_images.py /path/to/your/image/folder
Example:
- Windows:
python compress_images.py C:\Users\YourName\Pictures
- macOS/Linux:
python compress_images.py /home/yourname/pictures
- Windows:
-
The script will:
- Process supported images in the folder.
- Resize images to a maximum of 1024x1024 pixels (maintaining aspect ratio).
- Compress images to JPEG format with a quality setting of 50.
- Save compressed images in a
compressed
subfolder within the input folder. - Display original and compressed file sizes for each image.
-
Deactivate the virtual environment when done:
deactivate
You can modify the compress_images.py
script to adjust:
- Quality: Change the
quality
parameter (0–100) in thecompress_images
function. Lower values reduce file size but may decrease quality. - Maximum Size: Adjust the
max_size
parameter (e.g.,(1024, 1024)
) to set different maximum dimensions. - Supported Formats: Modify the
supported_formats
tuple to include/exclude image types.
Processed image1.jpg: 2048.50 KB -> 123.45 KB
Processed image2.png: 1500.30 KB -> 98.76 KB
Compressed images saved to /path/to/your/image/folder/compressed
- Pillow not found: Ensure Pillow is installed in the active virtual environment (
pip install Pillow
). - Permission errors: Run the terminal as administrator (Windows) or use
sudo
(macOS/Linux). - Invalid folder path: Verify the folder exists and contains supported image files.
- Pillow installation issues: Update pip (
pip install --upgrade pip
) and retry.