Skip to content

Instantly share code, notes, and snippets.

@curtisgibby
Last active January 17, 2022 10:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curtisgibby/ebac612657d6ef71a32fe03e75bd07cf to your computer and use it in GitHub Desktop.
Save curtisgibby/ebac612657d6ef71a32fe03e75bd07cf to your computer and use it in GitHub Desktop.
Powershell script to use imagemagick to convert a directory of SVGs to PNGs (with 10% padding)
$svgs = Get-ChildItem (".\*") -Filter *.svg
$scriptPath = $MyInvocation.MyCommand.Path
$scriptDirectory = Split-Path $scriptPath
$outputDirectory = $scriptDirectory + "\output"
# Replace specific path as needed
$magickExePath = "C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe"
ForEach($svg in $svgs) {
Write-Host $svg
$svgQuoted = '"' + $svg + '"'
$outputFile = Split-Path $svg.Basename -leaf
$outputFile = $outputFile + '.png'
$pngPath = Join-Path -Path $outputDirectory -ChildPath $outputFile
$pngPath = '"' + $pngPath + '"'
$arguments = 'convert','-scale','500x500','-extent','110%x110%','-gravity','center','-background','transparent',$svgQuoted,$pngPath
& $magickExePath $arguments
}
@mekinney
Copy link

Thank you. This seems very straight-forward but I tried a number of ways to send command line parameters to Magick from powershell, and this one finally worked.

@Nicicalu
Copy link

Thank your for the inspiration :)

@Willus8888
Copy link

Thank you.

Those that use it remember to create output folder first.

@jpeni
Copy link

jpeni commented Sep 26, 2021

How would I change this script to make image conversion RECURSIVE for all subfolders as well?

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