有没有办法将 SVG 批量导出为 PNG?

Uri*_*era 19 svg convert inkscape png

我有这些 SSVG,我想将它们导出为 PNG 图像,我可以使用 Inkscape 导出它们,但这意味着打开每个文件并将该文件导出为效率不高的 PNG(我有数百个)。

我怎样才能做到这一点?

Ser*_*gey 26

看来您可以从命令行使用 Inkscape:

`#{INKSCAPE_PATH} -z -f #{source_svg} -w #{width} -j -e #{dest_png}`
Run Code Online (Sandbox Code Playgroud)

更多细节

我想您可以编写一个简单的 bash 脚本来处理所有 SVG 文件:

#!/bin/sh

for file in *.svg
do
     /usr/bin/inkscape -z -f "${file}" -w 640 -e "${file}.png"
done
Run Code Online (Sandbox Code Playgroud)

上面的示例转换当前目录中的所有 .svg 文件,将 .png 扩展名添加到输出文件。

  • @Tosho 你也可以做`${file%svg}png`。您可以阅读 [此处](http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/string-manipulation.html) 了解更多可能性。 (2认同)

小智 18

受到先前接受的答案的启发,我想出了这个单行:

对于 Inkscape 0.92.4 及更早版本:

for file in *.svg; do inkscape $file -e ${file%svg}png; done
Run Code Online (Sandbox Code Playgroud)

这样你就不需要调用脚本了。如果你愿意,你可以创建一个别名来将当前目录中的所有 svgs 转换为 pngs:

alias svgtopng='for file in *.svg; do inkscape $file -e ${file%svg}png; done'
Run Code Online (Sandbox Code Playgroud)

对于 Inkscape 1.0 Beta 版及更高版本:

for file in *.svg; do inkscape $file -o ${file%svg}png; done
Run Code Online (Sandbox Code Playgroud)

这样你就不需要调用脚本了。如果你愿意,你可以创建一个别名来将当前目录中的所有 svgs 转换为 pngs:

alias svgtopng='for file in *.svg; do inkscape $file -o ${file%svg}png; done'
Run Code Online (Sandbox Code Playgroud)


Glu*_*ate 5

图形鹦鹉螺脚本


概述

命令行非常适合批量转换,但有时您只是不想离开舒适的 GUI。这就是为什么我编写了一个基于 GUI 的 Nautilus 脚本来将 SVG 文件批量转换为 PNG 图像。也应该支持其他具有自定义操作的文件管理器(例如 Thunar)。

截屏

在此处输入图片说明

脚本

#!/bin/bash

# NAME:         SVG2PNG
# VERSION:      0.1
# AUTHOR:       (c) 2014 Glutanimate (https://github.com/Glutanimate)
#
# DESCRIPTION:  Simple application to convert SVG files to PNG files based on DPI or resolution. 
#               Designed to work as a context menu script in file managers like Nautilus and Thunar.
#
# FEATURES:     - Converts SVG image file to PNG raster of a specific DPI or width
#               - SVG preview
#               - choose between converting the full SVG page or only the cropped image
#
# DEPENDENCIES: inkscape imagemagick yad
#               YAD (1) is an advanced for of Zenity with many improvements. It's not included in the
#               official Ubuntu repos yet (2) but can be installed from a webupd8 PPA (3)
#
# LICENSE:      MIT license (http://opensource.org/licenses/MIT)
#
# NOTICE:       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
#               INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
#               PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
#               LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
#               TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
#               OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# USAGE:        SVG2PNG image1.svg image2.svg [..]
#               I recommend installing this script as a context menu action for your file manager.
#               Instructions for Nautilus may be found on AskUbuntu (4).
#
# NOTES:        The script uses convert for previews because it's faster. For optimal results
#               the actual conversion is done with inkscape's command line interface.
#
# LINKS:        (1) https://code.google.com/p/yad/
#               (2) https://bugs.launchpad.net/ubuntu/+bug/796633
#               (3) https://launchpad.net/~webupd8team/+archive/y-ppa-manager
#               (4) https://askubuntu.com/questions/236414/how-can-i-install-a-nautilus-script

############## DIALOGS ###################

TITLE="SVG to PNG"
ICON="svg"

############## USGCHECKS #################

# checks if user selected an item

if [ $# -eq 0 ]
  then
      yad --title="$TITLE" \
          --image=dialog-error \
          --window-icon=dialog-error \
          --class="$WMCLASS" \
          --text="Error: no file selected" \
          --button="Ok":0
      echo "Error: no file selected"
      exit
fi

############### GLOBVAR ##################

SVGFILES="$@"
TEMPDIR=$(mktemp -d)
PREVIEWIMG="$TEMPDIR/svgpreview.png"

############### CLEANUP ##################

trap "rm -r $TEMPDIR" EXIT 

############## FUNCTIONS #################

converttosvg_dpi(){

echo "Converting based on DPI."

while [ $# -gt 0 ]; do

    echo "$# file(s) left to convert."
    SVGFILE="$1"
    FILESTEM="${SVGFILE%%.*}"
    PNGFILE="$FILESTEM".png
    inkscape "$SVGFILE" -z --export-dpi="$DPI" \
    --"$AREASETTING" --export-png="$PNGFILE"
    shift

done
echo "Done."

}

converttosvg_res(){

echo "Converting based on Width."

while [ $# -gt 0 ]; do

    echo "$# file(s) left to convert."
    SVGFILE="$1"
    FILESTEM="${SVGFILE%%.*}"
    PNGFILE="$FILESTEM".png
    inkscape "$SVGFILE" -z --export-width="$WIDTH" \
    --"$AREASETTING" --export-png="$PNGFILE"
    shift

done
echo "Done."

}

createpreview() {
convert -resize 128x "$1" "$PREVIEWIMG"
}

getsettings() {

SETTINGS=$(yad --window-icon "$ICON" --image "$PREVIEWIMG" --width 300 --height 200 --always-print-result \
--form --separator="|" --title="$TITLE" --text "Please choose the DPI or resolution to convert to." \
--field="DPI:NUM" 10[!80..600[!10]] --field="Width in px:NUM" 16[!16..4096[!16]] \
--field="Area:":CB "Drawing"\!"Page" \
--button="Convert based on DPI:2" --button="Convert based on Resolution:3" --button="gtk-cancel:1")

RET=$? # Exit code?

if [ "$RET" = 252 ] || [ "$RET" = 1 ]  # WM-Close or "Abort"
  then
      echo "Exiting..."
      exit
fi

DPI=$(printf %.0f $(cut -d "|" -f 1 <<<"$SETTINGS")) #round values
WIDTH=$(printf %.0f $(cut -d "|" -f 2 <<<"$SETTINGS"))
AREA=$(cut -d "|" -f 3 <<<"$SETTINGS")

case "$AREA" in

Drawing)
  AREASETTING="export-area-drawing"
  ;;

Page)
  AREASETTING="export-area-page"
  ;;

esac

echo "DPI set to $DPI"
echo "Width set to $WIDTH"
echo "Area set to $AREA"

}


################ MAIN ####################

createpreview "$1"
getsettings

case "$RET" in

2)
  echo 2
  converttosvg_dpi "$@"
  ;;

3)
  echo 3
  converttosvg_res "$@"
  ;;

esac

exit 0
Run Code Online (Sandbox Code Playgroud)

我会尽量更新这个答案,但请查看我的Github 存储库以获取最新版本的脚本。

安装

可以在此处找到所有 Nautilus 脚本的通用安装说明。以下命令应涵盖所有必要的依赖项:

sudo add-apt-repository ppa:webupd8team/y-ppa-manager
sudo apt-get update
sudo apt-get install yad inkscape imagemagick
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅上面的脚本标题。

用法

安装脚本后,您应该能够从文件管理器的上下文菜单中调用它。只需选择一个或多个 SVG 文件,然后单击上下文菜单中的相应条目即可。GUI 对话框应该提供几个关于转换的选项。

您可以根据 DPI 或宽度转换 SVG。在这两种情况下,纵横比都将保持不变。在单击转换按钮之前,请确保提供您选择的 DPI 或宽度。

您还可以选择导出完整的 SVG 文件或仅导出裁剪后的绘图。如果您的 SVG 画布有很多空白空间,建议选择“绘图”作为导出选项。