使用批处理文件中的 Inkscape 将 Svg 转换为 png 许多文件

Svg to png many files conversion using Inkscape from batch file

我有一个包含许多 svg 文件的文件夹,我想使用带有批处理脚本的 inkscape 将它们转换为 png 文件(Windows 10 *.bat 文件)。 以下脚本适用于单个文件,但是如何对目录中的数百个文件重复执行相同的命令?

PATH = "C:\Program Files\Inkscape"

inkscape myfile.svg --export-png=myfile.png
@echo off
for %%i in ("%~dp0*.svg") do (
    echo %%i to %%~ni.png
    "C:\Program Files\Inkscape\bin\inkscape.exe" --export-type="png" "%%i"
)

Wiki page, and in the man page(官方文档)中列出了其他导出选项:特别是,您可以设置 DPI、height/width、导出的区域等。

要利用所有逻辑核心来加快处理速度,您可以使用这种多任务处理方法:

@echo off
setlocal enableDelayedExpansion

:: Define the command that will be run to obtain the list of files to process
set listCmd=dir /b /a-d "%~dp0*.svg"

:: Define the command to run for each file, where "%%F" is an iterated file name from the list
::   something like YOUR_COMMAND -in "%%F" -out "%%~nF.ext"
set runCmd=""C:\Program Files\Inkscape\bin\inkscape.exe" --export-type="png" "%%F""

:: Define the maximum number of parallel processes to run.
set "maxProc=%NUMBER_OF_PROCESSORS%"

::---------------------------------------------------------------------------------
:: The remainder of the code should remain constant
::

:: Get a unique base lock name for this particular instantiation.
:: Incorporate a timestamp from WMIC if possible, but does not fail if
:: WMIC not available. Also incorporate a random number.
  set "lock="
  for /f "skip=1 delims=-+ " %%T in ('2^>nul wmic os get localdatetime') do (
    set "lock=%%T"
    goto :break
  )
  :break
  set "lock=%temp%\lock%lock%_%random%_"

:: Initialize the counters
  set /a "startCount=0, endCount=0"

:: Clear any existing end flags
  for /l %%N in (1 1 %maxProc%) do set "endProc%%N="

:: Launch the commands in a loop
  set launch=1
  for /f "tokens=* delims=:" %%F in ('%listCmd%') do (
    if !startCount! lss %maxProc% (
      set /a "startCount+=1, nextProc=startCount"
    ) else (
      call :wait
    )
    set cmd!nextProc!=%runCmd%
    echo -------------------------------------------------------------------------------
    echo !time! - proc!nextProc!: starting %runCmd%
    2>nul del %lock%!nextProc!
    %= Redirect the lock handle to the lock file. The CMD process will     =%
    %= maintain an exclusive lock on the lock file until the process ends. =%
    start /b "" cmd /c >"%lock%!nextProc!" 2^>^&1 %runCmd%
  )
  set "launch="

:wait
:: Wait for procs to finish in a loop
:: If still launching then return as soon as a proc ends
:: else wait for all procs to finish
  :: redirect stderr to null to suppress any error message if redirection
  :: within the loop fails.
  for /l %%N in (1 1 %startCount%) do 2>nul (
    %= Redirect an unused file handle to the lock file. If the process is    =%
    %= still running then redirection will fail and the IF body will not run =%
    if not defined endProc%%N if exist "%lock%%%N" 9>>"%lock%%%N" (
      %= Made it inside the IF body so the process must have finished =%
      echo ===============================================================================
      echo !time! - proc%%N: finished !cmd%%N!
      type "%lock%%%N"
      if defined launch (
        set nextProc=%%N
        exit /b
      )
      set /a "endCount+=1, endProc%%N=1"
    )
  )
  if %endCount% lss %startCount% (
    timeout /t 1 /nobreak >nul
    goto :wait
  )

2>nul del %lock%*
echo ===============================================================================

对于像我这样的人,他们在搜索 Linux 方法 以“批量”将 .svg 图片转换为 .pdf

在你的图片所在目录创建一个mysvg2pdf.sh可执行文件,代码如下

#!/bin/bash
mkdir "$PWD"/pdf
for file in $PWD/*.svg
    do
        filename=$(basename "$file")
        inkscape "$file" --export-type=pdf --export-filename="$PWD"/pdf/"${filename%}.pdf"
    done

执行它,./mysvg2pdf.sh一个子文件夹pdf被创建,其中包含相应的.pdf转换文件。

(来源:this answer

可能的代码改编

  • 如果你想用 inkscape 转换成另一种格式(如原来的问题 ;))只需将上面代码中字符串 pdf 的每个外观更改为你想要的格式,例如, png.
  • 如果您通过 flatpak 安装了 Inkscape,则将命令 inkscape 替换为表达式 /usr/bin/flatpak run --branch=stable --arch=x86_64 --command=inkscape --file-forwarding org.inkscape.Inkscape(其余部分保持不变,即, "$file" --export-type=...)
  • 如果您想去除文件的原始 .svg 扩展名,以便输出文件只有 .pdf 而不是 .svg.pdf,请将表达式 $(basename "$file") 更改为$(basename "$file" | cut -d. -f1)
  • 如果您想使用其他转换器,例如 python 转换器 cairosvg——我衷心推荐它,因为它生成的 .pdf 文件要小得多——。然后用 cairosvg 命令将整个 inkscape 命令行 inkscape "$file" ... "${filename%}.pdf" 更改为一行: cairosvg "$file" -o "$PWD"/pdf/"${filename%}.pdf"

Windows(撰写本文时为 v1.0)的较新版本的 Inkscape 发生了一些变化。 对于命令行,您可以使用 inkscape.com 而不是 .exe。这也适用于便携式版本。此外,参数--export-png 已被替换为--export-filename。 所以它应该看起来像这样:

FOR %%A IN (*.svg) DO "<path to Inkscape bin>\inkscape.com" %%A --export-filename=%%A.png
  • 应该从文件扩展名中自动检测导出类型。否则,您可以使用 --export-type=png.
  • 明确指定它
  • 对于缩放,您可以使用参数 --export-dpi=DPI,或通过 width/height --export-width=WIDTH --export-height=HEIGHT