TechEarl

How to Convert SVG to PNG From the Command Line

Convert SVG to PNG from the command line with rsvg-convert, Inkscape, or ImageMagick. Set the output size explicitly, keep transparency, and avoid the blurry low-density trap.

Ishan Karunaratne⏱️ 9 min readUpdated
Share thisCopied
Convert SVG to PNG from the command line with rsvg-convert, Inkscape, or ImageMagick. Set the output size explicitly, keep transparency, and avoid the blurry low-density trap.

The cleanest way to convert an SVG to a PNG from the command line is rsvg-convert, the renderer that ships with GNOME's librsvg:

bash
rsvg-convert -w 512 -h 512 in.svg -o out.png

That is the whole job for most cases. The one thing that trips everyone up is size: an SVG is scalable and has no inherent pixel dimensions, so you have to tell the converter how big the PNG should be. Leave -w/-h off and you get whatever "natural" size the SVG declares, which is often tiny or wrong. The rest of this page is the detail behind the three tools worth using (rsvg-convert, Inkscape, ImageMagick), when to reach for each, and the blurry-output trap that sends people in circles.

If you searched for svg2png (the old Node package), that tool is dead. It rendered through PhantomJS, which is unmaintained, so the package no longer keeps up. Use one of the maintained options below. For Node specifically, the modern replacement is sharp (which renders SVGs through librsvg under the hood), not svg2png.

rsvg-convert: the sharpest default

librsvg is a real SVG rendering library, and rsvg-convert is its CLI front end. It produces the cleanest, most spec-accurate rasterization of the three tools here, installs small, and runs fast. It is what I reach for first.

bash
# Render at an explicit pixel size
rsvg-convert -w 512 -h 512 in.svg -o out.png

# Scale proportionally so you do not squash the artwork
rsvg-convert -w 1024 --keep-aspect-ratio in.svg -o out.png

Two flags do the heavy lifting. -w/--width and -h/--height set the output pixel dimensions. --keep-aspect-ratio scales the image proportionally to fit those bounds, which is not the default (for backward compatibility, rsvg-convert will stretch to exactly -w by -h unless you ask it not to), so pass it whenever you set only one dimension or care about the proportions.

Install it where it is not already present:

bash
# macOS
brew install librsvg

# Debian / Ubuntu
sudo apt install librsvg2-bin

Output defaults to stdout if you omit -o, so rsvg-convert in.svg > out.png works too. SVGs are transparent by default and rsvg-convert preserves that transparency in the PNG with no extra flag.

Inkscape: highest fidelity for complex SVGs

When an SVG leans on features a lightweight renderer fumbles (filters, blend modes, complex gradients, embedded fonts, clip paths), Inkscape is the most faithful renderer because it is the same engine that draws the file in the editor. It is heavier to install and slower to start, so it is the tool for "the output does not match the design," not the everyday default.

The modern Inkscape 1.x CLI is different from the old 0.x syntax (the old -e/--export-png flags are gone), so make sure you are on the new form:

bash
# Inkscape 1.x: export type plus an explicit output width
inkscape in.svg --export-type=png -w 512

# Name the output file explicitly
inkscape in.svg --export-type=png --export-filename=out.png -w 512

--export-type=png selects the format. -w (short for --export-width) sets the output width in pixels; the height is computed automatically to keep the aspect ratio, and -h/--export-height is there if you need to pin both. With no -w, Inkscape falls back to the document's DPI hint, which is usually not what you want for a sized export. If you pass both --export-filename and --export-type, the filename extension has to match the type.

ImageMagick: convenient, but mind the density

ImageMagick is everywhere, so magick ... in.svg out.png is tempting. It works, but with a catch worth understanding because it is the single most common SVG-to-PNG confusion.

bash
magick -background none -density 384 in.svg out.png

Here is the key fact: ImageMagick does not rasterize the SVG itself if it can avoid it. It delegates to an external renderer, preferring librsvg or Inkscape if either is installed, and only falling back to its weak internal MSVG renderer if neither is present. So in practice you are often using librsvg anyway, just through a second layer.

-density is the part people get wrong. It is the rasterization resolution in DPI, and it must come before the input file to take effect. SVG has no pixels, so ImageMagick renders the vector at the given density and that, not a width flag, is what controls how crisp the result is. The default density is 96 DPI, which is why an unqualified magick in.svg out.png so often comes out blurry or pixelated: it rasterized at a low resolution and any later scaling just enlarged a small bitmap. Bump -density to render at the resolution you actually want (384 DPI is 4x the default), or skip the indirection entirely and use rsvg-convert, which sizes by pixels directly. -background none keeps the PNG transparent; without it, ImageMagick fills the SVG's empty areas with white.

"Not authorized" on SVG: the security policy

If magick in.svg out.png fails with attempt to perform an operation not allowed by the security policy 'SVG', the problem is not your command, it is ImageMagick's policy.xml. SVG (and its internal MSVG/MVG cousins) can pull in external files, so many managed, Docker, and distro-hardened installs ship a policy that blocks the coder outright with a line like:

xml
<policy domain="coder" rights="none" pattern="SVG" />

Find the active file with magick -list policy (it prints the path at the top). The clean fix is to install a real SVG renderer, librsvg or Inkscape, which ImageMagick then delegates to instead of touching the unsafe internal path; on most systems that alone clears the error. If you must loosen the policy, change that line to rights="read" rather than removing the policy file, and only on a host where you trust the SVGs you are converting. On a hardened or shared box, the safer move is to skip ImageMagick here and run rsvg-convert directly, which has none of this baggage.

Which one should I use?

  • rsvg-convert for almost everything: sharpest output, sizes by pixels, fast, small install.
  • Inkscape when a complex SVG (filters, blend modes, fonts) does not render correctly anywhere else.
  • ImageMagick when it is already installed and the SVG is simple, but remember to set -density and -background none, and know it is probably calling librsvg for you anyway.
  • Node project? Use sharp, not the dead svg2png. It renders SVGs through librsvg, same engine as rsvg-convert.

Keep the transparent background

SVGs are transparent by default, and a PNG supports an alpha channel, so the transparency should carry through. With rsvg-convert and Inkscape it does automatically. With ImageMagick you have to ask for it, or it flattens onto white:

bash
# Transparent (correct): SVG empty areas stay transparent in the PNG
magick -background none -density 384 in.svg out.png

# Solid background, if that is what you actually want
magick -background white -density 384 in.svg out.png

FAQ

See also

Sources

Authoritative references this article was fact-checked against.

Tagssvgpngrsvg-convertlibrsvgInkscapeImageMagickCLIrasterize

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years building software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Currently Chief Technology Officer at a healthcare tech startup, which is where most of these field notes come from.

Keep reading

Related posts

How to Convert a PSD to PNG From the Command Line

Convert a PSD to PNG from the command line with ImageMagick. The one trick that matters: design.psd[0] selects the flattened composite, so you get one PNG instead of a folder full of separate layers.