Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SVG to transparent PNG with antialiasing, using ImageMagick

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 the anti-aliasing, the edges always look terrible. Here's what I tried:

convert +antialias -background transparent  in.svg -resize 25x25 out.png

Any ideas or a different command line tool I could use?

like image 545
Andreas Gohr Avatar asked Mar 02 '12 09:03

Andreas Gohr


3 Answers

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
like image 143
Micah Avatar answered Nov 06 '22 06:11

Micah


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
like image 60
halfer Avatar answered Nov 06 '22 04:11

halfer


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.

like image 18
sleeper Avatar answered Nov 06 '22 05:11

sleeper