【问题标题】:Batch command for ImageMagick to convert all files in a directory and sub-directories on windowsImageMagick 的批处理命令在 Windows 上转换目录和子目录中的所有文件
【发布时间】:2015-08-05 12:26:12
【问题描述】:

我在一个文件夹和子文件夹中有数千个 SVG。我想要的是将它们全部批量转换为 jpgpng 图像。

有人可以帮我为 ImageMagick (windows) 编写一个命令,它可以找到所有 svg 并将其转换为原始名称的 jpg/png 并将它们保存在相同的目录中吗?

这是示例结构:

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

我希望转换后是这样的:

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

【问题讨论】:

标签: windows batch-file svg imagemagick imagemagick-convert


【解决方案1】:

您不需要 shell 脚本,只需使用 mogrify 命令

cd 到你的镜像目录

mogrify -format png *.svg

【讨论】:

  • 确实是最好的解决方案!使用-format 选项可防止文件被覆盖。此处的文档:imagemagick.org/script/mogrify.php
  • 还有-path选项,将输出写入不同的文件夹而不是覆盖
  • 找到 . -name '*.webp' -exec mogrify -format png {} \;
  • ImageMagick for Windows 似乎没有 mogrify 可执行文件,但你可以这样做:magick.exe mogrify -format png *.svg
  • 这应该是公认的答案
【解决方案2】:

尝试在根文件夹中使用带有/R 标志的FOR 循环:

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

此命令将转换您启动命令的根文件夹下子目录中的所有.svg 文件。

以上命令适用于命令行,如果您打算在批处理文件 (.bat) 中使用该命令,请记住使用 %% 而不是 %

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

有关更多信息,请参阅 Imagemagick 文档的 this 页面

【讨论】:

    【解决方案3】:

    当我在许多子目录中有许多图像要处理时,我从各种在线资源创建了以下脚本来转换文件。该脚本还包括进度显示。它已经使用 ImageMagick 7 进行了测试。希望对您有用。


    #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
    } 
    

    【讨论】:

      【解决方案4】:

      使用指定的源和目标路径将 PNG 批量转换为 JPG。

      mogrify \
      -path "target/dir/path/" \
      -quality 85% \
      -format jpg \
      "src/dir/path/*.png"
      

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 2015-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多