![](/img/trans.png)
[英]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.