如何在保持宽高比但改变范围的同时将 SVG 转换为 PNG?

How to convert SVG to PNG while keeping aspect ratio but changing extents?

我正在尝试将 SVG 转换为 PNG。 SVG 是一个 non-square 矩形。我希望图片的宽高比保持不变,所以需要填充图片的范围(透明)。

我在 OS X Mavericks,10.9.2.

我已经用 3 种工具试过了,但每一种都有问题:

使用 ImageMagick:

convert -background none -resize 200x200 -gravity center -extent 200x200 rect.svg rect-imagemagick.png 

开始--

结束--

图像是长宽比正确的正方形,但模糊不清。我看到其他用户报告了 imagemagick 将 SVG 转换为 PNG 时的这个问题。我已经将 imagemagick 升级为使用 rsvg(通过 brew),如以下版本的代表所示。

版本:

# convert --version
Version: ImageMagick 6.9.1-3 Q16 x86_64 2016-01-23 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: DPC Modules
Delegates (built-in): bzlib cairo fontconfig freetype jng jpeg ltdl lzma png rsvg tiff xml zlib

使用 rsvg:

rsvg-convert -a -w 200 -h 200 rect.svg > rect-rsvg.png

开始--

结束--

图像清晰且纵横比正确,但我没有找到使它变成方形的方法(扩展图像)。

版本:

# rsvg-convert --version
rsvg-convert version 2.40.9

使用 Inkscape:

inkscape -z -e rect-inkscape.png -w 200 -h 200 rect.svg

开始--

结束--

图片方正清晰,但纵横比不正确。

版本:

# inkscape --version
W: AppleCollationOrder setting not found, using AppleLocale.
Setting LANGSTR from AppleLocale: en
Overriding empty LANG from /usr/share/locale/locale.alias
Setting Language: en_US.UTF-8
Inkscape 0.48.5 r10040 (Jul 12 2014)

好吧,我意识到如果我将其中两个实用程序结合起来,答案很简单:

rsvg-convert -a -w 200 -h 200 rect.svg > rect-rsvg.png
convert -background none -resize 200x200 -gravity center -extent 200x200 rect-rsvg.png rect-correct.png

首先使用 rsvg-convert 转换为 PNG,它可以正确转换但不会扩展。然后使用 imagemagick 从 PNG 扩展到 PNG。