我想将文件夹
C:\Users\Eric\Desktop\svg
中的多个 SVG 文件转换为名称为 [SVG File Name].svg.png
的 512x512 PNG 文件。
我尝试了以下命令:
for /f %f in ('dir /b "C:\Users\Eric\Desktop\svg"') do inkscape -z -e %f.png -w 512 -h 512 %f
命令行正确检测 SVG 文件并遍历它们,但 Inkscape 会显示以下内容:
C:\Users\Eric\Desktop\inkscape>inkscape -z -e [SVG File Name].svg.png -w 512 -h 512 [SVG File Name].svg
** (inkscape.exe:8412): WARNING **: Can't open file: [SVG File Name].svg (doesn't exist)
** (inkscape.exe:8412): WARNING **: Can't open file: [SVG File Name].svg (doesn't exist)
** (inkscape.exe:8412): WARNING **: Specified document [SVG File Name].svg cannot be opened (does not exist or not a valid SVG file)
我在普通的 Inkscape 程序中打开了一个文件,它工作了。
对于 SVG 到 PNG 的转换,我发现 cairosvg (https://cairosvg.org/) 的性能比 ImageMagick 更好。在目录中的所有文件上安装和运行的步骤。
pip3 install cairosvg
在包含 .svg 文件的目录中打开 python shell 并运行:
import os
import cairosvg
for file in os.listdir('.'):
if os.path.isfile(file) and file.endswith(".svg"):
name = file.split('.svg')[0]
cairosvg.svg2png(url=name+'.svg',write_to=name+'.png')
这也将确保您不会覆盖原始 .svg 文件,但会保留相同的名称。然后,您可以使用以下命令将所有 .png 文件移动到另一个目录:
$ mv *.png [new directory]
Inkscape 是一个很好的程序。 有时我们不明白它所具有的可能性。 为了获得更好的性能,您应该使用 shell 模式。 此模式包含 2 个步骤:
type .\command.txt | inkscape --shell
(其中 command.txt
你的文件名)运行此文件。输入
action-list
后,所有命令都位于 inkscape --shell
中。
例如,如果您想将 SVG 转换为 png,您的 txt 文件应包含:
file-open:1.svg; export-filename:1.png; export-do; file-close
file-open:2.svg; export-filename:2.png; export-do; file-close
语法为
command
:arg
; command2
:arg2
;等等
您可以使用您最喜欢的语言(如 C++、Java、C# 或 Python)创建此文件。
附注
它比在 PowerShell 中对每个文件使用
inkscape
命令更快,但不要使用它进行长时间操作,因为它存在内存泄漏:
Gitlab 链接
Windows 10 上的 Inkscape 1.3:
for %G in ("*.svg") do ("C:\Program Files\Inkscape\bin\inkscapecom.com" --export-width=512 --export-height=512 --export-type=png %G)
inkscapecom.com
[原文如此]? com.com
?真的吗?!是的,真的。
至少,这是我今天(2023-09-12)下载的 Inkscape 1.3(版本 0e150ed6c4,2023-07-21)中的文件名。
我认为这是一个错误。有关详细信息,请参阅 Inkscape GitLab 问题 #8893,“Inkscape 1.3 中inkscape.com 替换为 inkscapecom.com”。
输出文件名:*.png
而不是
*.svg.png
我想要...名字如果您决定更喜欢
[SVG File Name].svg.png
[SVG File Name].png
,而不使用
.svg
,则将以下选项添加到命令行中:
--export-filename=%~nG.png
修饰符~n
提取基本文件名,不带文件扩展名。输出图像大小:相对而不是绝对
这是我使用的命令行:
for %G in ("*.svg") do ("C:\Program Files\Inkscape\bin\inkscapecom.com" --export-dpi=115 --export-background=#ffffff --export-background-opacity=1.0 --export-type=png --export-filename=%~nG.png %G)
就我而言:
export-dpi
选项将图像大小增加 1.2 倍(12 除以 10):
1.2 * 96 = 115.2其中 96 实际上是原始 SVG 的 DPI。