简体   繁体   English

ImageMagick 的批处理命令在 Windows 上转换目录和子目录中的所有文件

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

I have thousands of SVG's in a folder and sub-folders.我在一个文件夹和子文件夹中有数千个 SVG。 What I want is to batch convert all of them to jpg or png images.我想要的是将它们全部转换为jpgpng图像。

Can someone help me write a command for ImageMagick (windows), which can find and convert all the svg's to jpg/png with their original names and keep them in the same directories?有人可以帮我为 ImageMagick (windows) 编写一个命令,它可以找到所有 svg 并将其转换为原始名称的 jpg/png 并将它们保存在同一个目录中吗?

Here is the example structure:这是示例结构:

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

And I want it like this after conversion:转换后我希望它是这样的:

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

you don't need shell scripting just use the mogrify command您不需要 shell 脚本,只需使用mogrify命令

cd to your image directory cd 到你的图像目录

mogrify -format png *.svg

Try with a FOR loop with /R flag from inside your root folder:尝试在根文件夹中使用带有/R标志的FOR循环:

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.此命令将转换您启动命令的根文件夹下子目录中的所有.svg文件。

Above command works for command line, if you plan to use the command inside a batch file (.bat) remember to use %% instead of % :上述命令适用于命令行,如果您打算在批处理文件 (.bat) 中使用该命令,请记住使用%%而不是%

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

See this page of Imagemagick documentation for more info有关更多信息,请参阅 Imagemagick 文档的页面

To batch convert PNGs to JPGs with source and destination paths specified.使用指定的源和目标路径将 PNG 批量转换为 JPG。

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

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

To convert webp in batches with mogrify -format didn't work.使用 mogrify -format 批量转换 webp 不起作用。 So use this in current directory所以在当前目录中使用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在.bat文件(Windows)中使用ImageMagick调整所有子目录中所有图像的大小? - How would I use ImageMagick in a .bat file (Windows) to resize all images in all sub-directories? 如何使用批处理脚本从当前目录复制文件,而不复制子目录? - How do I copy files from the current directory, but not sub-directories using a batch script? 删除所有子目录和子文件而不删除父/根目录? - Remove All Sub-Directories and Sub-Files Without Removing Parent/Root Directory? 尝试使用Bat文件获取目录和子目录中所有文件的行数 - Trying to Get A Line Count for all Files in a Directory and Sub-Directories using a Bat File 批处理命令将文件从子目录移动到新目录 - Batch Command to Move Files from Sub Directories to New Directory 仅在目录,子目录中读取最近2个月的文件 - Read files for last 2 months only in directory, sub-directories 将“Everyone”组添加到目录及其所有子目录中 - Add group “Everyone” to directory and all of it's sub-directories 如何为文件夹中的所有子目录执行 git 拉取?(在 Windows 中) - How to do git pull for all sub-directories in a folder?(In Windows ) windows下将子目录和文件剪切复制到指定目录的命令 - command to cut and copy the sub directories and files to specified directory in windows 删除目录及其中的所有子目录 - removing directories and all sub-directories in it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM