Skip to content

agnivachat17/auto-file-compressor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SmartCompress

Real-time automatic multi-format file compression for Windows

Python Platform Status License

Watches a folder for supported files, compresses them with high visual quality, and saves them to an output folder β€” automatically, silently, forever.

πŸ’‘ Why I Built This

I wanted a lightweight local automation tool that could continuously compress files without relying on cloud services or manual processing.

Most existing tools were either:

  • online only
  • bloated
  • subscription-based
  • not automated

So I decided to build my own.


✨ What It Does

INPUT/                               OUTPUT/
  β”œβ”€β”€ report.pdf        ──────►      β”œβ”€β”€ comp_report.pdf       βœ” compressed
  β”œβ”€β”€ image.jpg         ──────►      β”œβ”€β”€ comp_image.jpg        βœ” compressed
  β”œβ”€β”€ photo.png         ──────►      β”œβ”€β”€ comp_photo.png        βœ” compressed
  β”œβ”€β”€ comp_old.pdf      (skip)       └── ...
  └── notes.txt         (ignore)
  • Monitors INPUT continuously using OS-level file events (no polling loop)
  • Processes existing supported files in INPUT on startup
  • Waits for file transfers to finish before compressing
  • Supports multiple formats:
    • PDF
    • JPG / JPEG
    • PNG
    • WEBP
  • Compresses PDFs with high visual quality optimisation
  • Compresses images while preserving original format
  • Renames output as comp_<originalname>
  • Saves compressed files to OUTPUT, leaving originals untouched
  • Ignores unsupported files and already-compressed files silently
  • Logs all activity to logs/watcher.log
  • Uses ~0% CPU while idle (fully event-driven)

🚧 Planned Features

  • Video compression
  • Drag-and-drop UI
  • Compression presets
  • EXE installer
  • Batch processing

⚑ Automation Features

This project was designed to run fully autonomously in the background with minimal user interaction.

Current automation capabilities include:

  • Silent background execution using pythonw
  • Windows startup automation via shell:startup
  • Optional Task Scheduler integration
  • Standalone executable support using PyInstaller
  • Continuous real-time folder monitoring
  • Automatic file detection and processing
  • Logging system for debugging and monitoring
  • Lightweight idle performance (~0% CPU usage)

The system has been repeatedly tested across:

  • system restarts
  • startup execution
  • silent background runs
  • EXE-based deployment workflows

πŸ“ Project Structure

SmartCompress/
β”‚
β”œβ”€β”€ watcher.py          ← Main program (monitoring + compression logic)
β”œβ”€β”€ config.py           ← YOUR settings (edit folder paths here)
β”œβ”€β”€ requirements.txt    ← Python package dependencies
β”‚
β”œβ”€β”€ run.bat             ← Launch with a visible terminal window (easy debugging)
β”œβ”€β”€ run_silent.pyw      ← Launch silently in background (no window)
β”œβ”€β”€ stop.bat            ← Stop the background process
β”œβ”€β”€ startup.vbs         ← Drop in Windows Startup folder for autostart on boot
β”‚
β”œβ”€β”€ build_exe.spec      ← Optional: build a standalone .exe with PyInstaller
β”‚
└── logs/
    └── watcher.log     ← All activity and errors are logged here

πŸš€ Setup (Step-by-Step)

Step 1 β€” Install Python

  1. Go to https://www.python.org/downloads/
  2. Click the big yellow "Download Python 3.x.x" button
  3. Run the installer
  4. ⚠️ Important: Check βœ… "Add Python to PATH" at the bottom of the first screen
  5. Click "Install Now"

To verify it worked, open Command Prompt (Win+R β†’ type cmd β†’ Enter) and run:

python --version

You should see something like Python 3.12.3.


Step 2 β€” Download / Place the Project

Save the entire SmartCompress folder somewhere permanent, for example:

C:\Users\YourName\Documents\SmartCompress\

Step 3 β€” Edit config.py

Open config.py in Notepad (right-click β†’ Open With β†’ Notepad).

Change these two lines to your actual folder paths:

FOLDER_A = r"C:\Users\YourName\Documents\INPUT"   # watch this folder
FOLDER_B = r"C:\Users\YourName\Documents\OUTPUT"   # save compressed files here

Tips:

  • Use r"..." (note the r before the quote) to avoid issues with backslashes
  • Both folders must exist before running (Folder B is auto-created if missing)
  • You can use any folder paths, including network drives like r"\\server\share\pdfs"

Optionally adjust compression quality (default 85 is excellent):

JPEG_QUALITY = 85    # 75 = smaller files, 92 = near-lossless

Step 4 β€” Install Dependencies

Double-click run.bat β€” it will automatically install all required packages the first time you run it.

Or install manually via Command Prompt:

cd C:\Users\YourName\Documents\SmartCompress
pip install -r requirements.txt

This installs:

Package Purpose
watchdog Efficient OS-level folder monitoring
pypdf Pure-Python PDF manipulation
Pillow Image recompression within PDFs

Step 5 β€” Run It!

Option A β€” With a terminal window (recommended for first use): Double-click run.bat

You'll see live output like:

2024-01-15 10:30:00  [INFO]  SmartCompress starting up
2024-01-15 10:30:00  [INFO]  Watching : C:\Users\You\Documents\INPUT
2024-01-15 10:30:00  [INFO]  Output   : C:\Users\You\Documents\OUTPUT
2024-01-15 10:30:00  [INFO]  β†’ Detected: quarterly_report.pdf
2024-01-15 10:30:01  [INFO]    βœ” Compressed: 4.2 MB β†’ 1.8 MB (57.1% smaller)
2024-01-15 10:30:01  [INFO]  Watching for supported files... (press Ctrl+C to stop)

Option B β€” Silent background (no window): Double-click run_silent.pyw

Nothing visible happens β€” the program runs quietly in the background. Check logs/watcher.log to confirm it's working.


πŸ”„ Run Automatically on Windows Startup

Recommended method: Windows Task Scheduler for silent startup automation.

startup.vbs is included as optional legacy support.

To start the compressor automatically every time you log into Windows:

  1. Press Win+R, type shell:startup, press Enter β†’ A folder opens (this is your Startup folder)

  2. Open startup.vbs in Notepad and change this line:

    SCRIPT_PATH = "C:\Users\YourName\Documents\SmartCompress\run_silent.pyw"

    to match where you saved the project.

  3. Copy startup.vbs into the Startup folder that opened in step 1.

  4. Test it: Double-click startup.vbs β€” the watcher should start silently. Check logs/watcher.log to confirm.

  5. On next login, it starts automatically. βœ…

To remove autostart: Delete startup.vbs from the Startup folder.

To stop the background process: Double-click stop.bat


πŸ“¦ Optional: Build a Standalone .exe

If you want to share this tool with someone who doesn't have Python installed, you can build a single self-contained executable.

Install PyInstaller:

pip install pyinstaller

Build:

cd C:\Users\YourName\Documents\SmartCompress
pyinstaller build_exe.spec

Result:

dist\watcher\watcher.exe

Share the entire dist\watcher\ folder. The recipient just needs to:

  1. Edit config.py inside the folder with their paths
  2. Double-click watcher.exe

No Python installation required on their machine. βœ…


πŸ“‹ Logs

All activity is saved to logs/watcher.log.

Example log output:

2024-01-15 09:00:00  [INFO]  ============================
2024-01-15 09:00:00  [INFO]  PDF Auto-Compressor starting
2024-01-15 09:00:00  [INFO]  Watching : C:\...\INPUT
2024-01-15 09:00:00  [INFO]  β†’ Detected: contract.pdf
2024-01-15 09:00:01  [INFO]    βœ” Compressed: contract.pdf  8.3 MB β†’ 2.1 MB (74.7% smaller)
2024-01-15 09:05:12  [INFO]  β†’ Detected: scan.pdf
2024-01-15 09:05:13  [INFO]    βœ” Compressed: scan.pdf  12.0 MB β†’ 3.4 MB (71.7% smaller)

The log file never grows unbounded β€” rotate it manually or let it grow (it's text, very compact).


πŸ›  Troubleshooting

Problem Solution
python not found Re-install Python and check "Add to PATH"
pip install fails Run Command Prompt as Administrator
File not being detected Check Folder A path in config.py is correct
Output looks wrong Raise JPEG_QUALITY in config.py to 92
No size reduction PDF may contain vector art only (no images to compress) β€” this is fine, the file is still losslessly optimised
Process won't stop Open Task Manager β†’ Details tab β†’ find pythonw.exe β†’ End Task

βš™οΈ How the Compression Works

The tool uses a two-stage approach for maximum quality-to-size ratio:

Stage 1 β€” Image recompression Each page's embedded images are extracted, decoded, re-encoded as JPEG at the configured quality level (default 85/100), and put back. Images are only replaced if the result is actually smaller. Vector graphics, text, and page structure are untouched.

Stage 2 β€” Lossless structural optimisation Duplicate PDF objects are merged, orphan objects are removed, and streams are compressed. This is always lossless and costs nothing in visual quality.

What's NOT changed:

  • Text content
  • Vector graphics / diagrams
  • Page structure and layout
  • Fonts
  • Metadata (author, title, etc.)
  • Original file (always kept)

πŸ“¦ Dependencies

Package Version License Why
watchdog 4.0.1 Apache 2.0 OS-level folder monitoring
pypdf 4.3.1 BSD PDF read/write, object deduplication
Pillow 10.3.0 HPND JPEG image recompression

All are permissively licensed and safe for personal and commercial use.


Built with Python 3.10+ β€’ Tested on Windows 10/11