Mark Ku's Blog

Origins of WebP

WebP was developed by Google. Its key advantage is delivering excellent visual clarity without bloating network payloads — which is critical for page load speed.

Why use WebP

For most websites, images account for over 60% of page weight. WebP can shrink JPEG file size by 35% and PNG file size by 50% while preserving quality and transparency. Adopting WebP reduces bandwidth usage and improves user experience.

File size comparison

The cwebp tool defaults to 80% quality. Here's a comparison between lossless 100% and the default 80%.

  1. JPG to WebP — Lossless WebP conversion actually grows the file size by roughly 2x. With the default 80% compression, however, file size drops by ~40%. File size comparison: JPG, lossless WebP, and 80% compressed Web
  2. PNG to WebP — Lossless WebP cuts file size by more than half. With the default 80%, files shrink by ~86%. Windows dialogs comparing PNG, lossless, and 80% compressed WebP file
  • An interesting observation:
  • The default 80% mode reduces both JPG and PNG sizes meaningfully, but the lossless 100% mode can actually double the size of a JPG.

Browser support

A few years ago WebP support was patchy, but it's now widely supported. One thing to watch out for: iOS only added WebP support starting with version 14, so older versions need a fallback.

Download Google's WebP tool

https://developers.google.com/speed/webp/download
Google's WebP download page with a highlighted download link

Write the PowerShell script (Convert-WebP.ps1)

$libPath = $PSScriptRoot + "\libwebp-1.2.0-windows-x64\bin\cwebp.exe"
$defaultFoldPath = (Get-Item .)
$outputFolder = 'output'
Function Get-Folder([String] $initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.rootfolder = "MyComputer"
    $foldername.SelectedPath = $initialDirectory
	$foldername.ShowNewFolderButton = $false
	
    if($foldername.ShowDialog() -eq "OK")
    {		
        $folder += $foldername.SelectedPath 
    }else{
		exit		
	}
    return $folder
}


Function Conver-Webp([String] $source, [String] $output)
{	
   Write-Host ('$input path:' + $source)
   Write-Host ('$output path:' + $output)

   if (!(Test-Path -Path $output )) {
   New-Item -ItemType directory -Path $output
   Write-Host 'create new folder' + $output
   }

   $files = get-childitem $source -recurse -force -include *.jpg, *.png  
   Write-Host $files
   foreach($inputFile in $files){      
	
   #Call cwebp  
   $inputFilePath = $inputFile.FullName  
   $outputFilePath = Split-Path -Path $inputFile.FullName  
   
   $outputFilePath = $output + "\" + [System.IO.Path]::GetFileNameWithoutExtension($inputFilePath) + ".webp"  
	
   # 使用 80% 品質等級去轉換單張圖片:

   Write-Host $outputFilePath  
   $args = "-q 80 " + $inputFilePath + " -o " + $outputFilePath  
  
   Start-Process -FilePath $libPath -ArgumentList $args -Wait  
  
   $originalFileSize = (Get-Item $inputFilePath).length  
   $optimizedFileSize = (Get-Item $outputFilePath).length    
     
   $savedBytes = $originalFileSize - $optimizedFileSize  
   $savedPercentage = [math]::Round(100-($optimizedFileSize / $originalFileSize)*100)  
   $message = $inputFilePath + " " + $outputFilePath + " " + $savedBytes + "bytes " + $savedPercentage + "%"  
   
   Write-Host $message   
   } 
}


$sourceFolderPath = Get-Folder($defaultFoldPath) 
$outputFolderPath =($sourceFolderPath+ '\' + $outputFolder)

Conver-Webp $sourceFolderPath $outputFolderPath 
	  
PAUSE

Place the downloaded WebP library next to the script, then right-click Convert-WebP.ps1 > Run with PowerShell

Windows context menu for Convert-WebP.ps1, 'Run with PowerShell' highlighted
Windows context menu for Convert-WebP.ps1, 'Run with PowerShell' highlighted

Select the source folder

Windows 'Select a folder' dialog, 'output' folder highlighted
Windows 'Select a folder' dialog, 'output' folder highlighted

Execution screen

PowerShell execution screen showing WebP conversion progress
PowerShell execution screen showing WebP conversion progress

When the script finishes, all jpg/png files are written to the output directory.

File Explorer displaying source folder containing various JPG PNG files
File Explorer displaying source folder containing various JPG PNG files

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··490

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··333

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··264

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··221

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··215

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11