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

前端 未结 3 694
时光说笑
时光说笑 2021-02-07 20:56

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

相关标签:
3条回答
  • 2021-02-07 21:13

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

    cd to your image directory

    mogrify -format png *.svg
    
    0 讨论(0)
  • 2021-02-07 21:25

    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

    0 讨论(0)
  • 2021-02-07 21:25

    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
    } 
    
    0 讨论(0)
提交回复
热议问题