Create Small Pictures without losing Size or Quality
This guide covers how to automate image optimization for a WordPress/Kadence site using a Windows-based local workflow. By converting high-res PNGs to WebP automatically, you can drop file sizes from 600KB to under 100KB without losing visual quality.
Guide: Automating WebP Conversion for Faster Web Loading
Phase 1: Install the Tools
To get started, you need the official Google WebP encoder and the Python environment to automate it.
- Install
cwebp:Open PowerShell as Administrator and run:PowerShellwinget install Google.Libwebp - Install Python Watchdog:This allows Python to “listen” for new files added to your folders.PowerShell
pip install watchdogNote: If you see a warning aboutwatchmedo.exenot being on PATH, you can safely ignore it.
Phase 2: Create the Automation Script
This script monitors a specific folder (C:\AI-PNG), converts any PNG to a WebP using high-efficiency settings, and places it in a dedicated subfolder.
- Create a folder at
C:\AI-PNG. - Inside that folder, create a file named
webp_watcher.pyand paste the following:
Python
import os, subprocess, time
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
SOURCE_DIR = r"C:\AI-PNG"
DEST_DIR = r"C:\AI-PNG\WEBP"
def convert_to_webp(file_path):
if file_path.suffix.lower() == '.png':
output_file = os.path.join(DEST_DIR, file_path.stem + ".webp")
if os.path.exists(output_file): return
# Settings: -q 75 (Quality), -m 6 (Slowest/Best compression), -af (Auto-filter)
cmd = ["cwebp", "-q", "75", "-m", "6", "-af", str(file_path), "-o", output_file]
try:
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print(f"Optimized: {file_path.name}")
except Exception as e:
print(f"Error: {e}")
class NewFileHandler(FileSystemEventHandler):
def on_created(self, event):
if not event.is_directory: convert_to_webp(Path(event.src_path))
if __name__ == "__main__":
if not os.path.exists(DEST_DIR): os.makedirs(DEST_DIR)
for item in os.listdir(SOURCE_DIR):
convert_to_webp(Path(os.path.join(SOURCE_DIR, item)))
observer = Observer()
observer.schedule(NewFileHandler(), SOURCE_DIR, recursive=False)
observer.start()
try:
while True: time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Phase 3: Set it to Run Forever (Windows Task Scheduler)
To ensure this runs in the background without a terminal window open:
- Find your Python path: Run
(Get-Command pythonw).Sourcein PowerShell. - Open Task Scheduler and Create Task.
- Trigger: Set to “At log on”.
- Action: “Start a Program”.
- Program/script: Paste the path to
pythonw.exe. - Add arguments:
C:\AI-PNG\webp_watcher.py - Start in:
C:\AI-PNG
- Program/script: Paste the path to
- Conditions: Uncheck “Start the task only if the computer is on AC power.”
Phase 4: Implementation in Kadence/WordPress
Now that you have your 75KB WebP files, here is how to use them:
- Upload Directly: WordPress natively supports WebP. Simply upload the files from your
WEBPfolder to the Media Library. - Kadence Image Block: Use the Kadence “Advanced Image” block to select your WebP. It will load significantly faster than the original 600KB PNG.
- Performance Result: Expect your Largest Contentful Paint (LCP) score to improve significantly, as feature images are usually the heaviest elements on a page.
Summary of Optimization Settings
| Flag | Purpose | Benefit |
-q 75 | Quality Factor | Significant size drop with no visible loss. |
-m 6 | Compression Method | Maximum CPU effort to find the smallest file size. |
-af | Auto-Filter | Smooths edges for a professional look on high-res displays. |