| | |

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.

  1. Install cwebp:Open PowerShell as Administrator and run:PowerShellwinget install Google.Libwebp
  2. Install Python Watchdog:This allows Python to “listen” for new files added to your folders.PowerShellpip install watchdog Note: If you see a warning about watchmedo.exe not 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.

  1. Create a folder at C:\AI-PNG.
  2. Inside that folder, create a file named webp_watcher.py and 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:

  1. Find your Python path: Run (Get-Command pythonw).Source in PowerShell.
  2. Open Task Scheduler and Create Task.
  3. Trigger: Set to “At log on”.
  4. 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
  5. 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 WEBP folder 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

FlagPurposeBenefit
-q 75Quality FactorSignificant size drop with no visible loss.
-m 6Compression MethodMaximum CPU effort to find the smallest file size.
-afAuto-FilterSmooths edges for a professional look on high-res displays.

Similar Posts