Skip to content

Instantly share code, notes, and snippets.

@RyKilleen
Last active May 5, 2024 20:09
Show Gist options
  • Save RyKilleen/6086c6a7b2ae06a3ea01396d79d637c7 to your computer and use it in GitHub Desktop.
Save RyKilleen/6086c6a7b2ae06a3ea01396d79d637c7 to your computer and use it in GitHub Desktop.
ImageMagick / Powershell: limit file size in parallel
$directory = "C:\full-path\to-photos-directory"
# Only .jpg files larger than 10mb
$files = Get-ChildItem -Path $directory -Filter *.jpg | Where-Object Length -gt 10mb
# Use ForEach-Object with the -Parallel parameter
$files | ForEach-Object -Parallel {
# Define the new filename with '_edited' suffix
$newFileName = [IO.Path]::Combine($_.DirectoryName, ($_.BaseName + "_edited" + $_.Extension))
# Execute ImageMagick command to resize the file
& magick convert $_.FullName -define jpeg:extent=10000kb $newFileName
} -ThrottleLimit 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment