How to convert a SVG to a PNG with Image Magick?

I have a SVG file which has a defined size of 16x16. When I use Image Magick's convert program to convert it into a PNG then I get a 16x16 pixel PNG which is way too small:

convert test.svg test.png

I need to specify the pixel size of the output PNG. -size parameter seems to be ignored, -scale parameter scales the PNG after it has been converted to PNG. The best result up to now I got by using the -density parameter:

convert -density 1200 test.svg test.png

But I'm not satisfied because I want to specify the output size in pixels without doing math to calculate the density value. So I want to do something like this:

convert -setTheOutputSizeOfThePng 1024x1024 test.svg test.png

So what is the magic parameter I have to use here?


I haven't been able to get good results from ImageMagick in this instance, but Inkscape does a nice job of it on Linux and Windows:

inkscape -z -e test.png -w 1024 -h 1024 test.svg

Here's the result of scaling a 16x16 SVG to a 200x200 PNG using this command:

Just for reference, my Inkscape version (on Ubuntu 12.04) is:

Inkscape 0.48.3.1 r9886 (Mar 29 2012)

and on Windows 7, it is:

Inkscape 0.48.4 r9939 (Dec 17 2012)

Try svgexport :

svgexport input.svg output.png 64x
svgexport input.svg output.png 1024:1024

svgexport is a simple cross-platform command line tool that I have made for exporting svg files to jpg and png, see here for more options. To install svgexport install npm, then run:

npm install svgexport -g

Edit: If you find an issue with the library, please submit it on GitHub, thanks!


This is not perfect but it does the job.

convert -density 1200 -resize 200x200 source.svg target.png

Basically it increases the DPI high enough (just use an educated/safe guess) that resizing is done with adequate quality. I was trying to find a proper solution to this but after a while decided this was good enough for my current need.

Note: Use 200x200! to force the given resolution

链接地址: http://www.djcxy.com/p/18314.html

上一篇: 在iOS中显示矢量图像(SVG或FXG)

下一篇: 如何使用Image Magick将SVG转换为PNG?