Convert SVG to transparent PNG with antialiasing, using ImageMagick

51,347

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

The way I learned how to do this was from the methodology found here: How to convert a .eps file to a high quality 1024x1024 .jpg?

It is the same idea as @halfer's solution with inkscape--to jack up the DPI first--but you can accomplish the same thing in just imagemagick using the -density option.

convert -density 200 in.svg -resize 25x25 -transparent white out.png

Solution 5

For me that works for svg to png:

convert ${src} \
    -transparent white \
    -background none \
    -resize 345x345 \
    res/drawable-xxxhdpi/${dest}
Share:
51,347
Andreas Gohr
Author by

Andreas Gohr

#SOreadytohelp

Updated on September 19, 2021

Comments

  • Andreas Gohr
    Andreas Gohr over 2 years

    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?

  • Martin Poljak
    Martin Poljak over 10 years
    At least at this time (6.8.6), it works with transparent too.
  • petrsyn
    petrsyn about 10 years
    Both 'none' and 'transparent' create opaque white background for me. Not transparent. I'm using 6.8.8-3 on Mac
  • commonpike
    commonpike about 10 years
    -background none worked for me, ImageMagick 6.8.6-6 on a mac
  • kostmo
    kostmo about 9 years
    @petrsyn although the command will accept the input image path as the first argument (e.g. convert in.svg -background none out.png, I found that this ordering will fail to make the background transparent; it resulted in an opaque white background! Make sure that in.svg and out.png are the final two arguments in the command.
  • timss
    timss about 6 years
    This removed the color palette of a SVG I tried to convert (nifi.apache.org/assets/images/apache-nifi-logo.svg).
  • timss
    timss about 6 years
    The first solution I've found that keeps the color palette of some SVGs, e.g. the logo of Apache NiFi (nifi.apache.org/assets/images/apache-nifi-logo.svg).
  • fmw42
    fmw42 over 5 years
    Imagemagick now will use Inkscape if it is installed on the system. You have the choice of: Inkscape (if on the system), RSVG delegate (must be installed with Imagemagick, or the Imagemagick internal MSVG/XML.
  • icc97
    icc97 over 5 years
    -background none is indeed correct as per the image magic color names documentation: "There is also a color called 'none' that is fully transparent. This color is shorthand for rgba(0, 0, 0, 0.0)."
  • icc97
    icc97 over 5 years
    This is the relevant documentation for the -transparent option: imagemagick.org/script/command-line-options.php#transparent
  • Domi
    Domi over 3 years
    After fiddling with it for a while, this was the only way to get it to work - much appreciated!
  • Fredrick Brennan
    Fredrick Brennan over 2 years
    This solution, while it may work for many SVG files, is inferior to the -background none solution, because white is a valid fill/stroke color that may present in many SVG's, and this turns all white pixels transparent, even those defined as being white fills/strokes.
  • Fredrick Brennan
    Fredrick Brennan over 2 years
    There is no reason to supply both -transparent white and -background none, and this will negatively effect files with elements of the foreground that are supposed to be white. Only supply -background none.
  • Fredrick Brennan
    Fredrick Brennan over 2 years
    The "supply -background none before the SVG filename" answer is much better because it actually uses ImageMagick, the tool asked about…: stackoverflow.com/a/18579465/1901658
  • Fredrick Brennan
    Fredrick Brennan over 2 years
    Rube Goldberg would be proud.
  • Fredrick Brennan
    Fredrick Brennan over 2 years
    -background none works just as well in recent IM releases, and avoids needing to supply -channel rgba if the target is PNG
  • halfer
    halfer over 2 years
    @FredrickBrennan: my general view is that readers (and question authors) will use the tool that gives them the best results. If an OP is made that asks how to knock in a nail with a screwdriver, sometimes an answer that recommends a hammer anyway is best, even if it is "not what they asked for".
  • halfer
    halfer over 2 years
    (Nice to hear there's more than one way to do it though 😌).
  • C Würtz
    C Würtz over 2 years
    I'm sorry @FredrickBrennan, I do not understand. Did you tried without the background rect on Imagick?
  • Fredrick Brennan
    Fredrick Brennan over 2 years
    Well, since user said they needed command line tool, I assume servers are involved, and ImageMagick is easier is much easier to run on servers, not requiring a little of Xorg dependencies. But yes, OK.
  • Fredrick Brennan
    Fredrick Brennan over 2 years
    Yes, you only need -background none option to convert.
  • Iulian Onofrei
    Iulian Onofrei over 2 years
    They got it backwards :|
  • pyramation
    pyramation over 2 years
    this works! if it doesn't you probably have a background inside of your SVG that you're unaware of! That took me a while to find out lol...