Skip to content

Instantly share code, notes, and snippets.

@Sevaarcen
Last active March 1, 2022 20:56
Show Gist options
  • Save Sevaarcen/739c0b3c2087ca1ef3712c3b19a8ce84 to your computer and use it in GitHub Desktop.
Save Sevaarcen/739c0b3c2087ca1ef3712c3b19a8ce84 to your computer and use it in GitHub Desktop.
Bulk Convert PNG to WEBP
# https://developers.google.com/speed/webp/download
Get-ChildItem -Path . -File -Depth 0 | ForEach-Object -Parallel { &'C:\Program Files\libwebp\bin\cwebp.exe' -q 75 "$_" -o "$([io.path]::GetFileNameWithoutExtension($_)).webp" } -ThrottleLimit 16
@bgoewert
Copy link

bgoewert commented Feb 10, 2022

Tried to do something similar but this is much simpler. Not big in PS. Didn't realize at the time there was a ForEach-Object.

Couple notes, -q defaults to 75, no need to define it otherwise. Also, I think you could use $_.BaseName instead of $([io.path]::GetFileNameWithoutExtension($_)). But to each their own.

What I'm using based off of this.

Get-ChildItem . -Depth 0 -Include "*.jpg","*.jpeg", "*.png", "*.tif" | ForEach-Object { cwebp "$_" -o "$($_.BaseName).webp" }

Also to do all sub-folders.

Get-ChildItem . -Recurse -Include "*.jpg","*.jpeg", "*.png", "*.tif" | ForEach-Object { cwebp "$_" -o "$($_.BaseName).webp" }

@bgoewert
Copy link

bgoewert commented Mar 1, 2022

Useful flags for bulk converting

-mt

  • For multi-threading, if possible.

-m 0-6

  • Specify the compression method to use. This parameter controls the trade off between encoding speed and the compressed file size and quality. Possible values range from 0 to 6. Default value is 4. When higher values are used, the encoder will spend more time inspecting additional encoding possibilities and decide on the quality gain. Lower value can result in faster processing time at the expense of larger file size and lower compression quality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment