Mark Ku's Blog

Webp 起緣

WebP圖像格式 起緣,由Google開發,它的主要優點是:使您獲得出色的清晰度而又不會增加網路傳輸的負擔。 這對於頁面加載速度至關重要。

為何要用 Webp

大多數網站,圖片佔網路加載的 60% 以上,然而 webp 格式的的出現,可以將 JPEG 圖像尺寸減小35%,將PNG圖像尺寸減小50%,並保留了其畫質和透明度,採用 webp 圖片格式,以減少網路的傳輸,提高使用者的體驗。

檔案大小比較

webp converter 預設是 80% 的壓縮格,這邊做了 無失真 100% 和壓縮格式 80% 的檔案大小的對比

  1. JPG 轉換 WEBP - 在無失真的 Webp 檔案轉換上,檔案大小不減反增,增加了約一倍以上的大小,但採用 Webp 預設80%的壓縮,檔案大小少了約 40%。 JPG轉WebP無失真與80%壓縮檔案大小比較
  2. PNG 轉換 WEBP - 在無失真的 Webp 檔案轉換上,檔案少了約一半以上,但採用 Webp 預設80%的壓縮,檔案大小少了約 86%。 PNG原圖與WebP無失真及80%壓縮檔案大小對
  • 但有有趣的現象
  • Webp 預設 80 % 的壓縮格式,確實 JPG / PNG 將圖片大小減少,但 Webp 的 100% 無失真格式,反而讓 JPG 檔案增加了一倍大小

瀏覽器支援度

Webp 在前幾年支援度還沒這麼好,現在支援度很普及了,但需要特別留意 iOS 14 後才支援 web p, 這邊就必須額外處理。

Google 下載 Webp tool

https://developers.google.com/speed/webp/download
Google WebP工具下載頁面,顯示libwebp下載連結

撰寫 power shell (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

將下載的 Webp Lib 放在 PowerShell 同目錄,對 Convert-WebP.ps1 > 右鍵 > 用 powershell 執行

對 Convert-WebP.ps1 右鍵選用 PowerShell 執行
對 Convert-WebP.ps1 右鍵選用 PowerShell 執行

選取來源資料夾

Windows 瀏覽資料夾對話框選取 output 資料夾
Windows 瀏覽資料夾對話框選取 output 資料夾

執行畫面

PowerShell 執行 JPG/PNG 轉 WebP 檔案轉換進度
PowerShell 執行 JPG/PNG 轉 WebP 檔案轉換進度

腳本執行完後,會將所有 jpg / png 的檔案 輸出到 output 目錄下。

檔案總管顯示source資料夾內多種圖片檔案與output資料夾
檔案總管顯示source資料夾內多種圖片檔案與output資料夾

作者

Mark Ku

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

覺得這篇有幫助?

作者做的免費工具、每日 Podcast 與電子報,都在這裡。

Mark Ku · 本文採用 CC BY 4.0 授權,轉載請註明作者並附上原文連結。

留言

訂閱電子報

訂閱後即時收到新文章通知,不錯過任何技術分享。

提交即表示同意接收電子報,隨時可

熱門文章

View all
Mark Ku
··603

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案
Mark Ku
··440

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南
Mark Ku
··323

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略
Mark Ku
··239

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1
Mark Ku
··222

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取
Mark Ku
··199

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調