How to convert a SVG to a PNG with ImageMagick?

前端 未结 18 2033
一个人的身影
一个人的身影 2020-11-28 17:04

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

18条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 17:46

    In ImageMagick, one gets a better SVG rendering if one uses Inkscape or RSVG with ImageMagick than its own internal MSVG/XML rendered. RSVG is a delegate that needs to be installed with ImageMagick. If Inkscape is installed on the system, ImageMagick will use it automatically. I use Inkscape in ImageMagick below.

    There is no "magic" parameter that will do what you want.

    But, one can compute very simply the exact density needed to render the output.

    Here is a small 50x50 button when rendered at the default density of 96:

    convert button.svg button1.png
    


    Suppose we want the output to be 500. The input is 50 at default density of 96 (older versions of Inkscape may be using 92). So you can compute the needed density in proportion to the ratios of the dimensions and the densities.

    512/50 = X/96
    X = 96*512/50 = 983
    


    convert -density 983 button.svg button2.png
    


    In ImageMagick 7, you can do the computation in-line as follows:

    magick -density "%[fx:96*512/50]" button.svg button3.png
    
    or
    
    in_size=50
    in_density=96
    out_size=512
    
    magick -density "%[fx:$in_density*$out_size/$in_size]" button.svg button3.png
    

提交回复
热议问题