[英]How would I use ImageMagick in a .bat file (Windows) to resize all images in all sub-directories?
[英]Batch command for ImageMagick to convert all files in a directory and sub-directories on windows
我在一个文件夹和子文件夹中有数千个 SVG。 我想要的是将它们全部转换为jpg或png图像。
有人可以帮我为 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
您不需要 shell 脚本,只需使用mogrify
命令
cd 到你的图像目录
mogrify -format png *.svg
尝试在根文件夹中使用带有/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 文档的此页面
使用指定的源和目标路径将 PNG 批量转换为 JPG。
mogrify \
-path "target/dir/path/" \
-quality 85% \
-format jpg \
"src/dir/path/*.png"
当我在许多子目录中有许多图像要处理时,我从各种在线资源创建了以下脚本来转换文件。 该脚本还包括进度显示。 它已经用 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
}
使用 mogrify -format 批量转换 webp 不起作用。 所以在当前目录中使用它
for f in *.jpg ; do magick "$f" -quality 50 -define webp:lossless=false "$f".webp ; done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.