自动化“简化路径”对于 svg 文件(使用 inkscape)

发布于 2024-12-03 01:04:43 字数 148 浏览 1 评论 0原文

我想自动化 inkscape 命令“简化路径”。具体来说,我想要一个命令行工具,它将 svg 文件作为输入,将“简化路径”应用于图中的所有路径,并保存一个新的(较小的)svg 文件。使用 inkscape 可以吗?有没有一个免费的命令行工具(我使用的是linux)可以完成这项工作?

I would like to automatize the inkscape command "simplify path". Concretely, I would like a command line tool which takes a svg-file as input, applies "simplify path" to all paths in the figure and saves a new (smaller) svg-file. Is this possible using inkscape? Is there a free command line tool (I'm using linux) which does the job?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

向日葵 2024-12-10 01:04:43

更新 (2023-06-07):

v1.2 中的命令行再次发生变化。现在它应该类似于(未经测试!):

inkscape backpacks-svgrepo-com.svg --batch-process --actions='select-all;path-simplify;export-plain-svg'

您可以通过运行以下命令获取所有操作的所有列表:

inkscape --action-list

并选择适合您需求的操作。保存有点棘手,因为它没有在操作中列出。手册上是这么说的:

此外,还有与所有导出选项匹配的操作(要使用它们,只需删除选项前面的前缀“--”并将“=”替换为“:”)


更新(2021):

由于问题/答案很旧,inkscape 命令行已更改。

inkscape file.svg --batch-process --actions='EditSelectAll;SelectionSimplify;FileSave;FileClose'

另请参阅 Oren Ben-Kiki 或 Pix 答案的评论。

ORIG:

应该可以:

http://tavmjong .free.fr/INKSCAPE/MANUAL/html/CommandLine.html

展示了如何从命令行调用 inkscape 的函数(称为“动词”)。要获取所有动词的列表,请在命令行上调用 inkscape --verb-list 。您正在寻找的是SelectionSimplify

因此,您必须编写一个小脚本,过滤 svg 中的每个 id,并使用 id 调用 inkscape。像这样的东西(优化所有路径并最后退出 inkscape)

inkscape filename.svg --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose --verb=FileQuit

UPDATE (2023-06-07):

Again the command line changed in v1.2. Now it should be something like (untested!):

inkscape backpacks-svgrepo-com.svg --batch-process --actions='select-all;path-simplify;export-plain-svg'

You can get a list of all list of all actions by running:

inkscape --action-list

and chose those fitting to your needs. Saving is a bit more tricky as it's not listed in the actions. The manual says something like:

In addition, there are actions matching all export options (to use them, simply remove the prefix '--' in front of the option and replace '=' with ':')


UPDATE (2021):

Since the question/answer is quite old the inkscape command line changed.

inkscape file.svg --batch-process --actions='EditSelectAll;SelectionSimplify;FileSave;FileClose'

Also see comment of Oren Ben-Kiki or Pix answer.

ORIG:

Should be possible:

http://tavmjong.free.fr/INKSCAPE/MANUAL/html/CommandLine.html

shows how to call functions of inkscape (called "verbs") from the command line. To get a list of all verbs call inkscape --verb-list on commandline. What you are looking for is SelectionSimplify.

Therefore you have to write a small script that is filtering every id out of the svg and is calling inkscape with the ids. Something like this (optimize all pathes and quit from inkscape at the end)

inkscape filename.svg --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose --verb=FileQuit
氛圍 2024-12-10 01:04:43

Fabian 的答案延伸,为了控制简化函数的阈值,我发现我需要制作一个假主目录带有包含我所需阈值的最小首选项文件。这是我刚刚整理的一个简单脚本。

简化.sh:

#!/bin/bash
FILENAME=$1
THRESHOLD=$2
FAKEHOME=$(mktemp -d)
mkdir -p $FAKEHOME/.config/inkscape
cat > $FAKEHOME/.config/inkscape/preferences.xml <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<inkscape
  xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
  xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  version="1">
  <group
    id="options">
    <group
      id="simplifythreshold"
      value="${THRESHOLD}" />
  </group>
</inkscape>
EOF
# for Inkscape < 1.0
#HOME=$FAKEHOME inkscape $FILENAME --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose
# for Inkscape > 1.0
HOME=$FAKEHOME inkscape --with-gui --batch-process $FILENAME --verb='EditSelectAll;SelectionSimplify;FileSave'
#rm -rf $FAKEHOME

Extending from Fabian's answer, to control the threshold of the simplification function, I found I needed to make a fake home directory with a minimal preferences file containing my desired threshold. Here is a simple script I just put together.

simplify.sh:

#!/bin/bash
FILENAME=$1
THRESHOLD=$2
FAKEHOME=$(mktemp -d)
mkdir -p $FAKEHOME/.config/inkscape
cat > $FAKEHOME/.config/inkscape/preferences.xml <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<inkscape
  xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
  xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  version="1">
  <group
    id="options">
    <group
      id="simplifythreshold"
      value="${THRESHOLD}" />
  </group>
</inkscape>
EOF
# for Inkscape < 1.0
#HOME=$FAKEHOME inkscape $FILENAME --verb=EditSelectAll --verb=SelectionSimplify --verb=FileSave --verb=FileClose
# for Inkscape > 1.0
HOME=$FAKEHOME inkscape --with-gui --batch-process $FILENAME --verb='EditSelectAll;SelectionSimplify;FileSave'
#rm -rf $FAKEHOME
自由如风 2024-12-10 01:04:43

Inkscape 的替代方案

我使用 SVGO 获得了更好的结果(减少了文件从 2.7 MB 到 350 KB)。

您可以针对单个文件使用此在线服务:https://jakearchibald.github.io/svgomg/

Alternative to Inkscape

I've got much better results using SVGO (reduced a file from 2.7 MB to 350 KB).

You may use this online service for individual files: https://jakearchibald.github.io/svgomg/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文