Skip to content

Muzammil803/image-compressor-python-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Image Compression Script

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.

Features

  • 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.

Requirements

  • 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.

Setup

1. Install Python

Ensure Python 3.6 or higher is installed. Download from python.org if needed. Verify installation:

python --version

2. Create a Virtual Environment

A virtual environment is recommended to manage dependencies:

  1. Navigate to your project directory:
    mkdir image_compression
    cd image_compression
  2. Create a virtual environment named venv:
    python -m venv venv
  3. Activate the virtual environment:
    • Windows:
      venv\Scripts\activate
    • macOS/Linux:
      source venv/bin/activate

3. Install Pillow

With the virtual environment activated, install Pillow:

pip install Pillow

Verify installation:

pip show Pillow

4. Save the Script

Save the compress_images.py script (provided separately) in your project directory.

Usage

  1. Prepare a folder containing images (e.g., JPG, PNG).

  2. 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
  3. 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.
  4. Deactivate the virtual environment when done:

    deactivate

Customization

You can modify the compress_images.py script to adjust:

  • Quality: Change the quality parameter (0–100) in the compress_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.

Example Output

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

Troubleshooting

  • 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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages