Skip to content

Instantly share code, notes, and snippets.

@nidbCN
Last active February 20, 2025 16:16
Show Gist options
  • Select an option

  • Save nidbCN/b2a9146e0aa9f4a011f5de0ad4a2e8b9 to your computer and use it in GitHub Desktop.

Select an option

Save nidbCN/b2a9146e0aa9f4a011f5de0ad4a2e8b9 to your computer and use it in GitHub Desktop.
Blog image compress and standardizing script
$TARGET_WIDTH = 1620
$widthDictionary = @{}
$maxWidth = 0
foreach ($f in Get-ChildItem -File *.jpg, *.png, *.bmp) {
$info = magick identify -format "%w" "$($f.Name)"
$width = $info -as [int]
$widthDictionary[$f.Name] = $width
Write-Output "Compare $($f.Name) size $width with $maxWidth"
if ($width -gt $maxWidth) {
$maxWidth = $width
}
}
$scale = $TARGET_WIDTH / $maxWidth
Write-Output "Max width:$maxWidth, scale $scale x"
foreach($f in Get-ChildItem -File *.jpg, *.png, *.bmp) {
$originName = $f.Name
$targetName = $f.Name.Replace($f.Extension, ".webp")
$targetWidth = $widthDictionary[$f.Name] * $scale -as [int]
Write-Output "Process $originName scale to $targetWidth"
magick "$originName" `
-quality 50 `
-define webp:lossless=false `
-define webp:auto-filter=true `
-define webp:image-hint=graph `
-define webp:method=6 `
-colorspace RGB `
-resize $targetWidth `
-gravity center `
-background none `
-extent $TARGET_WIDTH `
-colorspace sRGB `
"$targetName"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment