在Windows上使用ImageMagick批处理命令将目录及其子目录中的所有文件转换

42

我有成千上万个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

3
以下是需要翻译的内容:http://www.imagemagick.org/Usage/windows/#for_recursive 或 http://www.imagemagick.org/Usage/windows/#for_loops - JosefZ
5个回答

113

你不需要使用shell脚本,只需使用mogrify命令即可。

切换到您的图像目录。

mogrify -format png *.svg

2
确实是最佳解决方案!使用-format选项可以防止文件被覆盖。文档在此处:https://imagemagick.org/script/mogrify.php - kotchwane
5
还有一个 -path 选项,可以将输出写入不同的文件夹而不是覆盖。 - Nobody
4
查找所有名为“*.webp”的文件,并使用mogrify将它们转换为png格式。 - Gregory Alan Bolcer
7
在Windows系统下,ImageMagick似乎没有 mogrify 可执行文件,但是您可以使用以下命令:magick.exe mogrify -format png *.svg - NightElfik
@NightElfik,这个命令是 mogrify -format png *.svg,不需要在开头加上 magick.exe - Vera de Kok
此解决方案破坏了预定义画布尺寸的SVG分辨率。 - JeremyKun

11

批次转换PNG格式为JPG格式,需要指定源路径和目标路径。

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

8
请尝试使用“FOR”循环和“/R”标志,从您的根文件夹内部开始:

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

这条命令将转换根目录下所有子目录中的.svg 文件。

如果你计划在批处理文件(.bat)中使用该命令,请记得使用%%代替%

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

请参考Imagemagick文档的此页面了解更多信息。

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
} 

2

使用 mogrify -format 批量转换 webp 格式无法正常工作。因此,请在当前目录中使用以下命令:

for f in *.jpg ; do magick "$f" -quality 50 -define webp:lossless=false "$f".webp ; done

1
为什么mogrify对你没起作用? - mwfearnley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接