我有这些 SVGS,我想将它们导出为 PNG 图像,我可以使用 Inkscape 导出它们,但这意味着打开每个文件并将该文件导出为 PNG,效率不高(我有数百个)。
我怎样才能做到这一点?
答案1
看来您可以从命令行使用 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 扩展名。
答案2
受到先前接受的答案的启发,我想出了这句话:
对于 Inkscape 0.92.4 及更早版本:
for file in *.svg; do inkscape "$file" -e "${file%svg}png"; done
这样你就不需要调用脚本了。如果你愿意,你可以创建一个别名,将当前目录中的所有 svg 转换为 png:
alias svgtopng='for file in *.svg; do inkscape "$file" -e "${file%svg}png"; done'
对于 Inkscape 1.0 Beta 版及更高版本:
for file in *.svg; do inkscape "$file" -o "${file%svg}png"; done
这样你就不需要调用脚本了。如果你愿意,你可以创建一个别名,将当前目录中的所有 svg 转换为 png:
alias svgtopng='for file in *.svg; do inkscape "$file" -o "${file%svg}png"; done'
答案3
图形化 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 文件或仅导出裁剪后的绘图。如果您的 SVG 画布有大量空白空间,建议选择“绘图”作为导出选项。
答案4
如果不是所有文件,但只有某些 SVG 文件需要转换为 PNG,人们可能用于sed
自动生成文件名:
inkscape --without-gui --export-width=1280 --export-png=`echo $1 |sed -e 's/svg$/png/'` $1