Batch command for ImageMagick to convert all files in a directory and sub-directories on windows
Asked Answered
M

5

45

I have thousands of SVG's in a folder and sub-folders. What I want is to batch convert all of them to jpg or png images.

Can someone help me write a command for ImageMagick (windows), which can find and convert all the svg's to jpg/png with their original names and keep them in the same directories?

Here is the example structure:

C:\SVG\BusinessMan.svg
C:\SVG\Models\Home.svg
C:\SVG\Underlines\underline.svg

And I want it like this after conversion:

C:\SVG\BusinessMan.svg
C:\SVG\BusinessMan.jpg
C:\SVG\Models\Home.svg
C:\SVG\Models\Home.jpg
C:\SVG\Underlines\underline.svg
C:\SVG\Underlines\underline.jpg
Munro answered 23/5, 2015 at 15:19 Comment(1)
D
8

Try with a FOR loop with /R flag from inside your root folder:

FOR /R %a IN (*.svg) DO convert "%~a" "%~dpna.jpg"

this command will convert all the .svg files in your sub-directories under the root folder you launched your command from.

Above command works for command line, if you plan to use the command inside a batch file (.bat) remember to use %% instead of %:

FOR /R %%a IN (*.svg) DO convert "%%~a" "%%~dpna.jpg"

See this page of Imagemagick documentation for more info

Diabetes answered 29/5, 2015 at 22:15 Comment(0)
F
119

you don't need shell scripting just use the mogrify command

cd to your image directory

mogrify -format png *.svg
Faucal answered 24/1, 2020 at 8:13 Comment(6)
Best solution indeed ! Using the -format option prevents the file from being overwritten. Documentation here : imagemagick.org/script/mogrify.phpRind
There is also the -path option, to write output to a different folder instead of overwritingJin
find . -name '*.webp' -exec mogrify -format png {} \;Alric
ImageMagick for Windows does not seem to have mogrify executable, but you can do for example: magick.exe mogrify -format png *.svgCroup
@Croup it's mogrify -format png *.svg without the magick.exe at the startCentavo
This solution destroys the resolution for svgs with a pre-defined canvas size.Seem
L
14

To batch convert PNGs to JPGs with source and destination paths specified.

mogrify \
-path "target/dir/path/" \
-quality 85% \
-format jpg \
"src/dir/path/*.png"
Lowrie answered 16/11, 2021 at 21:53 Comment(0)
D
8

Try with a FOR loop with /R flag from inside your root folder:

FOR /R %a IN (*.svg) DO convert "%~a" "%~dpna.jpg"

this command will convert all the .svg files in your sub-directories under the root folder you launched your command from.

Above command works for command line, if you plan to use the command inside a batch file (.bat) remember to use %% instead of %:

FOR /R %%a IN (*.svg) DO convert "%%~a" "%%~dpna.jpg"

See this page of Imagemagick documentation for more info

Diabetes answered 29/5, 2015 at 22:15 Comment(0)
C
4

I created the following script from various online resources to convert files when I had many images in many subdirectories to process. This script also includes a progress display. It has been tested with ImageMagick 7. I hope you find it useful.


#ImageMagick Recursive Powershell Script with Progress display
#This script will execute a command recursively on all folders and subfolders
#This script will display the filename of every file processed
#set the source folder for the images
$srcfolder = "C:\temp"
#set the destination folder for the images
$destfolder = "C:\temp"
#set the ImageMagick command
$im_convert_exe = "magick"
#set the source image format (wildcard must be specified)
$src_filter = "*.png"
#set the destination (output) image format
$dest_ext = "bmp"
#set the ImageMagick command options
$options = "-colorspace rgb -density 300 -depth 8 -alpha off"
#set the log file path and name
$logfile = "C:\temp\convert.log"
$fp = New-Item -ItemType file $logfile -force
#The following lines allow the display of all files that are being processed
$count=0
foreach ($srcitem in $(Get-ChildItem $srcfolder -include $src_filter -recurse))
{
$srcname = $srcitem.fullname
$partial = $srcitem.FullName.Substring( $srcfolder.Length )
$destname = $destfolder + $partial
$destname= [System.IO.Path]::ChangeExtension( $destname , $dest_ext )
$destpath = [System.IO.Path]::GetDirectoryName( $destname )

if (-not (test-path $destpath))
{
    New-Item $destpath -type directory | Out-Null
}
#the following line defines the contents of the convert command line
$cmdline =  $im_convert_exe + " `"" + $srcname  + "`"" + $options + " `"" + $destname + "`" " 
#the following line runs the command
invoke-expression -command $cmdline  
$destitem = Get-item $destname
$info = [string]::Format( "{0} `t {1} `t {2} `t {3} `t {4} `t {5}", $count, $partial, $srcname, $destname, $srcitem.Length, $destitem.Length )
echo $info
Add-Content $fp $info
$count=$count+1
} 
Crack answered 18/5, 2020 at 22:36 Comment(0)
M
2

To convert webp in batches with mogrify -format didn’t work. So use this in current directory

for f in *.jpg ; do magick "$f" -quality 50 -define webp:lossless=false "$f".webp ; done
Marashio answered 27/6, 2022 at 11:5 Comment(1)
Why didn’t mogrify work for you?Zhang

© 2022 - 2025 — McMap. All rights reserved.