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. Plus PSD to PNG with ImageMagick's [0] composite trick.

Ishan KarunaratnePublished ⏱️ 13 min readUpdated
Share thisCopied
Convert SVG to PNG from the command line with rsvg-convert, Inkscape, or ImageMagick (size, density, transparency), plus PSD to PNG with the [0] composite.

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, the blurry-output trap that sends people in circles, and the Photoshop equivalent: converting a PSD to PNG without Photoshop.

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?

To convert SVG to PNG from the command line, use rsvg-convert unless you have a reason not to: for an ordinary static SVG it gives clean, sharp output, sizes by pixels, and is a small, fast install. Switch to Inkscape only when a complex SVG (filters, blend modes, fonts) renders wrong everywhere else, and keep ImageMagick for simple SVGs on a machine where it is already installed, with -density and -background none set.

  • 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

Convert a PSD to PNG

Photoshop files are the other "turn this design file into a PNG" job, and ImageMagick reads PSD natively, no Photoshop required. The command you want is:

bash
magick "design.psd[0]" design.png

The [0] is the whole trick. A PSD is not one image: it is a stack of layers plus a pre-rendered flattened version that Photoshop bakes in when it saves, and ImageMagick exposes each of those as an indexed frame, the same way it treats the frames of an animated GIF. design.psd[0] is the merged composite (the artwork as you see it in Photoshop); design.psd[1], design.psd[2] and up are the individual layers. Leave the index off and magick design.psd design.png exports every frame, writing design-0.png, design-1.png, design-2.png, one per layer. That is the classic surprise.

Quote the filename: the square brackets are glob characters in most shells. On older 6.x installs the command is convert "design.psd[0]" design.png, and convert still works as a compatibility shim on 7.

Extract a single PSD layer, or every layer

The same index gives you any layer on its own:

bash
magick "design.psd[2]" layer2.png

To deliberately export every layer to its own numbered PNG, drop the index and use a %d placeholder:

bash
magick design.psd layer-%d.png

That writes layer-0.png (the composite), layer-1.png, layer-2.png, and so on.

-flatten as the alternative

To have ImageMagick rebuild the composite from the layers itself instead of reading Photoshop's saved one, skip image 0 and flatten only the layers:

bash
magick "design.psd[1--1]" -background none -flatten design.png

The index matters here too. A bare magick design.psd -flatten reads the saved composite as image 0 and every layer after it, then stacks the layers on top of an image that is already fully merged, so you are not really rebuilding anything. [1--1] means "image 1 through the last", which drops the composite (and if the PSD has no separate layers, ImageMagick hands back the first image instead). -background none keeps the canvas transparent; -flatten otherwise fills it with the default white.

For most files [0] and the layer flatten land on the same result, but they get there differently: [0] reads the composite Photoshop already saved, while flattening composites the layers in ImageMagick. With layer masks or unusual blend modes the two can diverge, and the saved composite is usually the more faithful match. Reach for the layer flatten when [0] looks wrong or the composite is missing.

PNG keeps an alpha channel, so [0] preserves the composite's transparency as long as the PSD had it. If the PNG comes out with a solid background, that background is in the composite's pixels, and no output flag removes it (-alpha on only switches an alpha channel on; it does not make any opaque pixel transparent). Check whether the composite carries transparency at all with magick identify -format '%A\n' "design.psd[0]" (False means no alpha). If it does not, hide the background layer in Photoshop and re-save, or rebuild from the layers with the [1--1] command above.

Batch convert a folder of PSDs

bash
for f in *.psd; do
  magick "${f}[0]" "${f%.psd}.png"
done

${f%.psd}.png swaps the extension, so hero.psd becomes hero.png. For very large PSDs, cap memory if a batch run balloons, for example magick -limit memory 2GiB "${f}[0]" out.png.

The Maximize Compatibility caveat

ImageMagick reads the saved composite for [0], and that composite is only guaranteed to be present and correct if the file was saved from Photoshop with Maximize Compatibility turned on (the "Maximize PSD and PSB File Compatibility" preference). With it off, the saved composite may be stale or missing, and a PSD that leans on adjustment layers, smart objects, or non-trivial blend modes can render wrong: washed-out color, missing effects, or a result that does not match Photoshop. ImageMagick reads the file rather than running Photoshop's rendering engine, so anything not baked into the saved composite will not be reproduced. No flag fixes this; re-save the PSD with Maximize Compatibility on. It is the most common reason a command-line PSD conversion looks off, and it is not an ImageMagick bug.

PSD to PNG on macOS with sips

On a Mac without ImageMagick, the built-in sips lists psd among its supported input formats:

bash
sips -s format png design.psd --out design.png

sips reads Photoshop's saved composite too, so the Maximize Compatibility caveat applies. It has no layer-index concept, so it only ever gives you the flattened image, never individual layers.

FAQ

Almost always because you used ImageMagick at its default density. SVG has no pixels, so ImageMagick rasterizes at 96 DPI unless told otherwise, and any scaling after that just enlarges a small bitmap. Put -density (e.g. -density 384) before the input file, or use rsvg-convert -w 512 -h 512, which sizes by pixels directly and renders sharp.

An SVG is scalable and has no fixed pixel dimensions, so you must set the output size explicitly. With rsvg-convert use -w/-h; with Inkscape use -w (export width); with ImageMagick the size follows from -density. Without any of these you get the SVG's declared natural size, which is often not what you expect.

No. svg2png rendered through PhantomJS, which is unmaintained, so the package stopped keeping up and is effectively dead. For Node, use sharp, which rasterizes SVGs through librsvg (the same engine as rsvg-convert). On the command line, reach for rsvg-convert, Inkscape, or ImageMagick instead.

rsvg-convert and Inkscape preserve the SVG's transparency automatically. ImageMagick flattens onto white unless you pass -background none, so add that flag for a transparent PNG.

That error comes from ImageMagick's policy.xml, not your command: many hardened, Docker, and distro installs block the SVG coder because it can read external files. The safe fix is to install librsvg or Inkscape so ImageMagick delegates to a real renderer instead of the unsafe internal path. Run magick -list policy to find the active policy file; if you must loosen it, set the SVG line to rights="read" and only on a host where you trust the input. Or sidestep it entirely with rsvg-convert.

Because ImageMagick does not rasterize SVGs well on its own. It delegates to an external renderer, preferring librsvg or Inkscape when present and only falling back to its weak internal MSVG renderer when neither is installed. Installing librsvg gives ImageMagick a real SVG engine to call.

Because you left the index off. A PSD contains a flattened composite plus every individual layer, and ImageMagick exports all of them by default. Use magick "design.psd[0]" design.png to select just the composite.

The PSD was almost certainly saved without Photoshop's Maximize Compatibility option, so the composite ImageMagick reads is stale or missing. Re-save it from Photoshop with Maximize Compatibility on, then convert again. Neither ImageMagick nor macOS sips needs Photoshop installed to read the file.

Loop over them and take the composite from each: for f in *.psd; do magick "${f}[0]" "${f%.psd}.png"; done. That writes one PNG per PSD, keeping the original base name.

Sources

Authoritative references this article was fact-checked against.

Tagssvgpngpsdrsvg-convertlibrsvgInkscapeImageMagickPhotoshopCLIrasterize

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts

Toggle macOS dark mode from the terminal with one osascript line, set the volume, turn Bluetooth on or off, take a camera photo and read your calendar.

How to Toggle Dark Mode From the macOS Command Line

Toggle macOS dark mode from the terminal with one osascript line: instant, no restart. Force it on or off, read the current state, and skip the dead _HIEnableThemeSwitchHotKey trick. Plus the other Mac settings from the command line: system volume (osascript), Bluetooth (blueutil), a photo with the Mac camera (imagesnap) and your calendar (cal, icalBuddy).