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

34,760

Solution 1

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

Solution 2

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

cd to your image directory

mogrify -format png *.svg

Solution 3

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
} 

Solution 4

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"
Share:
34,760
Key
Author by

Key

Updated on July 09, 2022

Comments

  • Key
    Key almost 2 years

    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