Skip to content

sahil007-ai/Image_compressor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bulk Image Compressor

A simple yet powerful Python script to compress all JPEG and PNG images within a folder to a specified target file size.

Overview

This script automates the process of reducing the file size of images in bulk. It iteratively adjusts the JPEG quality of each image until its size is below a defined threshold, making it perfect for preparing images for web use, email attachments, or simply saving disk space.

Features

Batch Processing: Compresses all images in a specified directory.
Target Size Compression: Aims for a specific file size (in kilobytes) for each image.
Handles Transparency: Automatically converts PNGs with transparency (RGBA/P modes) to RGB for JPEG compatibility.
Iterative Quality Reduction: intelligently lowers image quality in small steps to find the best balance between size and quality.
Error Handling: Skips corrupted or problematic files without crashing.
Cross-Platform: Runs on any system with Python and Pillow installed.

Requirements

• Python 3.x
• Pillow library

You can install the Pillow library using pip:

pip install Pillow

How to Use

Clone or download the script: Save the Python code to a file (e.g., compressor.py).
Organize your images: Place all the images you want to compress into a single folder.
Configure the script: Open the script and modify the following variables at the bottom of the file:

Example usage
input_folder = 'path/to/your/images'
output_folder = 'path/to/save/compressed_images'
target_size_kb = 250 # Target size for each image in kilobytes

Replace 'path/to/your/images' with the actual path to your input folder.
• Replace 'path/to/save/compressed_images' with the path where you want the compressed images to be saved. The script will create this folder if it doesn't exist.
Adjust target_size_kb to your desired maximum file size.

4. Run the script: Execute the script from your terminal:
python compressor.py

5. Check the output: The compressed images will be saved in the specified output folder, and the script will print the final size of each processed image in the console.

How It Works

The script iterates through each file in the input folder. For each image, it starts with a high quality setting (85/100) and saves a JPEG version. It then checks the file size. If it's larger than the target, it reduces the quality by 5 and saves it again. This loop continues until the file size is below the target or the quality level reaches a minimum threshold (5/100), preventing excessive degradation.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Bulk Image Compressor A simple yet powerful Python script to compress all JPEG and PNG images within a folder to a specified target file size.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages