简体   繁体   中英

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

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

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

cd to your image directory

mogrify -format png *.svg

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

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"

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
} 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM