有没有办法批量导出SVG到PNG?

我有这些SVGS,我想将它们导出到PNG图像,我可以用Inkscape导出它们,但这意味着打开每个文件并将该文件导出到PNG,这是无效的(我有数百个)。

我怎样才能做到这一点?

对不起necroposting,但今天我正在寻找一个解决方案,并受到我提出的这个单线的接受答案的启发:

for file in *.svg; do inkscape $file -e ${file%svg}png; done 

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

 alias svgtopng='for file in *.svg; do inkscape $file -e ${file%svg}png; done' 

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

 `#{INKSCAPE_PATH} -z -f #{source_svg} -w #{width} -j -e #{dest_png}` 

更多细节

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

 #!/bin/sh for file in *.svg do /usr/bin/inkscape -z -f "${file}" -w 640 -e "${file}.png" done 

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

图形Nautilus脚本


概观

命令行非常适合批量转换,但有时您只是不想放弃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 

我将尝试更新此答案,但请查看我的Github存储库以获取最新版本的脚本。

安装

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

 sudo add-apt-repository ppa:webupd8team/y-ppa-manager sudo apt-get update sudo apt-get install yad inkscape imagemagick 

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

用法

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

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

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

这是一个稍微不同的替代解决方案,使用更易读的脚本语言 – python。 它可以批量导出你所有的svgs。 特别理想,如果你正在做Android开发,并且必须从单个svg制作多个png。

免责声明:我写了lib。 希望它可以帮到某人。

点击这里

为简单起见,请将库下载到文件夹中,将svgs放在同一文件夹中,然后运行

 python exporter.py 

cd到文件夹后在命令行/终端中。 有关更高级的选项,请查看自述文件 。

如果不是所有文件,但只需要将某些SVG文件转换为PNG,则可以使用sed自动生成文件名:

 inkscape --without-gui --export-width=1280 --export-png=`echo $1 |sed -e 's/svg$/png/'` $1