【问题标题】:Automatizing 'simplify path' for a svg-file (using inkscape)自动化 svg 文件的“简化路径”(使用 inkscape)
【发布时间】:2011-09-04 13:39:54
【问题描述】:

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

【问题讨论】:

    标签: svg inkscape


    【解决方案1】:

    更新:

    由于问题/答案很旧,因此 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

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

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

    【讨论】:

    • 问我对How to save SVG file with Inkscape CLI?的回答,所谓CLI模式的动词需要GUI,不要使用-z--without-gui
    • 这似乎在某些 Inkscape 版本中被破坏 - 它失败并出现 GUI required 错误。似乎也不再有 FileQuit 动词?
    • 从 1.0.2 开始的正确方法似乎是 inkscape file.svg --batch-process --actions='EditSelectAllSelectionSimplify;FileSave;FileClose'。如果在 Windows 上,这是 inkscape.com 而不是 inkscape.exe
    【解决方案2】:

    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
    

    【讨论】:

    • 感谢您的脚本。对于当前版本的 Inkscape,我必须添加 --with-gui 和 --batch-process 标志以使其工作,并将动词分组到一个 --verb 参数中。我编辑了你的答案——希望你不介意。似乎 GUI 和图形环境现在是必需的。
    【解决方案3】:

    Inkscape 的替代品

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

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

    【讨论】:

    • SVGO 是否有一个选项可以像inkscape“简化”那样实际简化路径信息?除非我遗漏了什么,否则它似乎只对编码进行无损优化。
    • @pix 你应该直接在 SVGO 上问这个问题:github.com/svg/svgo/issues
    • @Adrian 我猜这是因为您的答案仅包含指向库/程序的链接,而没有进一步说明如何使用它或解决此特定问题。
    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2015-03-12
    相关资源
    最近更新 更多