Bulk convert PNG/JPG → WebP

Step 1: Download & Install cwebp

Go to Google’s official WebP tools: https://developers.google.com/speed/webp/download Download Windows (x64) precompiled binaries. Extract the ZIP. Inside you’ll see a bin folder containing cwebp.exe. Move that folder somewhere safe, e.g.: C:\webp\

Step 2: Add cwebp to PATH (so PowerShell recognizes it)

Press Win + R, type: sysdm.cpl Step 3: Test In PowerShell, run: cwebp -version 👉 If it prints a version number, it’s working 🎉

Step 3: Run the Script

Open PowerShell in the folder containing your images. Run: .\convert-to-webp.ps1 👉 It will generate optimized .webp files for all .jpg/.jpeg/.png. 👉 You can tweak $quality (lower = smaller size) and $resizeWidth (e.g. 1280) to shrink even more

🔧 Step 4: Re-run Your Script

Now your script will work: .\convert-to-webp.ps1 → Enter. Go to Advanced > Environment Variables. In System variables, find Path → Edit. Click New and add: C:\webp\bin (or wherever you extracted). Click OK → OK → OK. Restart PowerShell (important).

# Bulk convert PNG/JPG → WebP (optimized like TinyPNG) $quality = 75 # adjust 60-85 for balance $method = 6 # 0=fast, 6=slow/better compression $resizeWidth = 0 # set >0 to resize (e.g. 1280) $files = Get-ChildItem -Include *.jpg, *.jpeg, *.png -Recurse foreach ($file in $files) { $output = $file.DirectoryName + "\" + $file.BaseName + ".webp" if ($resizeWidth -gt 0) { cwebp -q $quality -m $method -mt -resize $resizeWidth 0 $file.FullName -o $output } else { cwebp -q $quality -m $method -mt $file.FullName -o $output } Write-Host "Converted: $($file.Name) → $($output)" } https://chatgpt.com/c/68bf9729-c8c0-8327-a15a-83e1857f9e92

No comments:

Post a Comment

Pages