我有这些SVGS,我想将它们导出到PNG图像,我可以用Inkscape导出它们,但这意味着打开每个文件并将该文件导出到效率不高的PNG(我有数百个)。
我怎样才能做到这一点?
我有这些SVGS,我想将它们导出到PNG图像,我可以用Inkscape导出它们,但这意味着打开每个文件并将该文件导出到效率不高的PNG(我有数百个)。
我怎样才能做到这一点?
Answers:
受到先前接受的答案的启发,我想到了以下这一点:
对于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'
看来您可以从命令行使用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扩展名。
总览
命令行非常适合批量转换,但是有时您只是不想离开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) /ubuntu/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画布有很多空白,建议选择“绘图”作为导出选项。
如果不是所有文件,而是仅某些SVG文件需要转换为PNG,则可以使用sed
该文件自动生成文件名:
inkscape --without-gui --export-width=1280 --export-png=`echo $1 |sed -e 's/svg$/png/'` $1
${file%svg}png
是个绝招!我以前没看过。