Skip to content

Repository files navigation

Image Compression Project

Comparative Analysis and Implementation of Lossless vs. Lossy Image Compression Algorithms

A comprehensive Python project that compares lossless (PNG) and lossy (JPEG) image compression techniques with an interactive web interface using Streamlit.


Features

Lossless Compression - PNG format with optimization
Lossy Compression - JPEG format (quality=50) with optimization
Batch Processing - Process multiple images at once
Interactive Dashboard - Web-based UI with Streamlit
Visualizations - Charts and graphs for analysis
CSV Reports - Generate detailed compression reports
Error Handling - Graceful handling of corrupted images
Support - JPG, JPEG, PNG, and BMP formats


Installation

1. Clone or Navigate to Project Directory

cd Image_Compression_Project

2. Install Dependencies

pip install -r requirements.txt

Or manually install:

pip install Pillow pandas streamlit plotly

Usage

Option 1: Command Line (Console Output)

Run the standard Python script for console-based compression:

python main.py

Output:

  • Compressed images in compressed_lossless/ and compressed_lossy/ folders
  • CSV report in results/report.csv
  • Console summary with statistics

Option 2: Interactive Web Dashboard (Recommended)

Run the Streamlit application:

streamlit run streamlit_app.py

Browser Access:

  • Opens automatically at http://localhost:8501
  • If not, manually navigate to the URL shown in terminal

Streamlit Dashboard Features

📊 View Results Tab

  • View all compression results from the CSV report
  • Interactive data table
  • Key statistics (total images, average reductions, total saved)
  • Download report as CSV
  • Three visualization tabs:
    • Size Comparison - Bar chart comparing original vs compressed sizes
    • Reduction % - Bar chart showing compression reduction percentages
    • Compression Ratio - Scatter plot of compression ratios

🖼️ Compress Images Tab

  • Upload individual images for compression
  • View before/after comparison
  • Get instant compression metrics
  • Download compressed versions

📁 Batch Process Tab

  • Process all images in the images/ folder
  • Progress bar during processing
  • Automatic CSV report generation
  • View results with statistics and visualizations

Project Structure

Image_Compression_Project/
│
├── images/                          # Place your images here
│   └── (add JPG, JPEG, PNG, BMP files)
│
├── compressed_lossless/             # Lossless PNG outputs
├── compressed_lossy/                # Lossy JPEG outputs
├── results/                         # CSV reports
│   └── report.csv
│
├── main.py                          # CLI application
├── streamlit_app.py                 # Web dashboard
├── compression.py                   # Compression functions
├── analysis.py                      # Analysis functions
├── requirements.txt                 # Python dependencies
└── README.md                        # This file

File Descriptions

compression.py

Contains compression functions:

  • compress_lossless(input_path, output_path) - PNG compression with optimization
  • compress_lossy(input_path, output_path) - JPEG compression with quality=50

analysis.py

Contains analysis functions:

  • compression_ratio(original_size, compressed_size) - Calculates compression ratio
  • size_reduction_percentage(original_size, compressed_size) - Calculates reduction %

main.py

CLI application that:

  • Scans images/ folder for supported formats
  • Processes each image with both compression methods
  • Generates results/report.csv
  • Displays formatted summary table

streamlit_app.py

Interactive web dashboard with:

  • Three operational modes (View Results, Compress Images, Batch Process)
  • Real-time image previews
  • Interactive visualizations
  • Download capabilities

Compression Algorithms

Lossless Compression (PNG)

  • Format: PNG (Portable Network Graphics)
  • Optimization: PIL optimize=True
  • Use Case: Lossless compression, suitable for graphics and images requiring perfect quality
  • Typical Result: 2-50% size reduction (varies by image type)

Lossy Compression (JPEG)

  • Format: JPEG (Joint Photographic Experts Group)
  • Quality Setting: 50 (0-100 scale)
  • Optimization: PIL optimize=True
  • RGB Conversion: Automatic handling of transparency and color modes
  • Use Case: Significant file size reduction, acceptable for photographs
  • Typical Result: 50-95% size reduction

Metrics Explained

Compression Ratio

Formula: Original Size / Compressed Size

  • Ratio = 1.0 - No compression
  • Ratio > 1.0 - Successfully compressed
  • Ratio < 1.0 - File expanded (poor compression)

Size Reduction Percentage

Formula: ((Original - Compressed) / Original) × 100

  • Positive % - File reduced in size
  • Negative % - File increased in size

Example Output

CLI Output

[1/1] Processing: black-adam-the-fire-bg.jpg
  ✓ Lossless: -127.92% reduction
  ✓ Lossy: 86.67% reduction

======= STATISTICS =======
Total images processed:        1
Average lossless reduction:    -127.92%
Average lossy reduction:       86.67%

CSV Report (results/report.csv)

Image Name,Original Size (KB),Lossless Size (KB),Lossless Compression Ratio,Lossless Reduction %,Lossy Size (KB),Lossy Compression Ratio,Lossy Reduction %
black-adam-the-fire-bg.jpg,7110.55,16206.11,0.44,-127.92,947.54,7.5,86.67

Error Handling

The application includes robust error handling:

  • Corrupted images are skipped with error message
  • Unsupported formats are ignored
  • Missing folders are created automatically
  • Invalid inputs are validated

Troubleshooting

Streamlit not found

pip install streamlit

PIL (Pillow) errors

pip install --upgrade Pillow

No images found

  • Ensure images are placed in the images/ folder
  • Check supported formats: JPG, JPEG, PNG, BMP

CSV Report not generated

  • Ensure results/ folder exists (auto-created on run)
  • Check disk space and file permissions

Performance Notes

  • Processing Speed: Depends on image size and quantity
  • Memory Usage: Moderate (loads one image at a time)
  • Disk Space: Compressed files stored simultaneously

Future Enhancements

  • Additional compression algorithms (WebP, AVIF)
  • Quality slider for lossy compression
  • Real-time preview updates
  • Batch upload with drag-and-drop
  • Advanced filtering and sorting
  • Export to multiple formats

License

This project is provided as-is for educational purposes.


Author

Created as a comprehensive image compression analysis project.

Last Updated: June 2026

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages