How to convert a SVG to a PNG with ImageMagick?

前端 未结 18 1823
一个人的身影
一个人的身影 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:48

    On Linux with Inkscape 1.0 to convert from svg to png need to use

    inkscape -w 1024 -h 1024 input.svg --export-file output.png
    

    not

    inkscape -w 1024 -h 1024 input.svg --export-filename output.png
    
    0 讨论(0)
  • 2020-11-28 17:49

    In order to rescale the image, the option -density should be used. As far as I know the standard density is 72 and maps the size 1:1. If you want the output png to be twice as big as the original svg, set the density to 72*2=144:

    convert -density 144 source.svg target.png
    
    0 讨论(0)
  • 2020-11-28 17:51

    One thing that just bit me was setting the -density AFTER the input file name. That didn't work. Moving it to the first option in convert (before anything else) made it work (for me, YMMV, etc).

    0 讨论(0)
  • 2020-11-28 17:53

    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!

    0 讨论(0)
  • 2020-11-28 17:53

    For simple SVG to PNG conversion I found cairosvg (https://cairosvg.org/) performs better than ImageMagick. Steps for install and running on all SVG files in your directory.

    pip3 install cairosvg
    

    Open a python shell in the directory which contains your .svg files and run:

    import os
    import cairosvg
    
    for file in os.listdir('.'):
        name = file.split('.svg')[0]
        cairosvg.svg2png(url=name+'.svg',write_to=name+'.png') 
    

    This will also ensure you don't overwrite your original .svg files, but will keep the same name. You can then move all your .png files to another directory with:

    $ mv *.png [new directory]
    
    0 讨论(0)
  • 2020-11-28 17:56

    why don't you give a try to inkscape command line, this is my bat file to convert all svg in this dir to png:

    FOR %%x IN (*.svg) DO C:\Ink\App\Inkscape\inkscape.exe %%x -z --export-dpi=500 --export-area-drawing --export-png="%%~nx.png"

    0 讨论(0)
提交回复
热议问题