Convert SVG to transparent PNG with antialiasing, using ImageMagick

后端 未结 8 882
耶瑟儿~
耶瑟儿~ 2020-12-07 13:06

I want to convert SVG images to PNG files with transparent background and anti-aliased edges (using semi-transparent pixels). Unfortunately I can\'t get ImageMagick to do th

相关标签:
8条回答
  • 2020-12-07 13:41

    As a side note, I found that getting transparency was a bit tricky. Instead of using transparent, I had to use none.

    convert -background none in.svg out.png
    
    0 讨论(0)
  • 2020-12-07 13:41

    Actually, reading imagemagick documentation:

    -antialias

    Enable/Disable of the rendering of anti-aliasing pixels when drawing fonts and lines. By default, objects (e.g. text, lines, polygons, etc.) are antialiased when drawn. Use +antialias to disable the addition of antialiasing edge pixels. This will then reduce the
    number of colors added to an image to just the colors being directly drawn. That is, no mixed >colors are added when drawing such objects.

    the +antialias will indeed disable antialiasing.

    0 讨论(0)
  • 2020-12-07 13:41

    For me that works for svg to png:

    convert ${src} -transparent white -background none  -resize 345x345 res/drawable-xxxhdpi/${dest}
    
    0 讨论(0)
  • 2020-12-07 13:51

    I get better, already nicely antialiased results if I replace -resize with -scale. Then, the antialias flag isn't even necessary.

    0 讨论(0)
  • 2020-12-07 13:56

    Inkscape will do this:

    inkscape \
        --export-png=out.png --export-dpi=200 \
        --export-background-opacity=0 --without-gui in.svg
    

    Update

    The terminology has changed: all the export params suppress gui, and the output parameter is now simply based on the file type. For example, a type of png will cause a file in /path/to/picture.svg to be exported as /path/to/picture.png (caution: this overwrites output).

    inkscape \
        --export-type=png --export-dpi=200 \
        --export-background-opacity=0 picture.svg
    

    Note cited wiki has quotes on --export-type=png, which is incorrect.

    Also if don't have Inkscape command line, MacOS can access via bash directly:

    /Applications/Inkscape.app/Contents/MacOS/inkscape
    
    0 讨论(0)
  • 2020-12-07 13:57

    Adding the -transparent white option solves the problem particularly in my case because background isn't removed completely (unfortunately light shadow is present). So I'm using IMHO more clearer solution that fully removes background with ImageMagic:

    convert -channel rgba -background "rgba(0,0,0,0)" in.svg out.png

    It sets a fully transparent black color as the background through the RGBA channel.

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