23

I'm on Linux and I'd like to take an SVG file and bake it into a PNG file at a given resolution (scaling). Is there a way to do this quickly and effectively from the command line?

I'm looking for something like this:

svg2png --width 128 --height 128 input.svg output.png

Is there a tool for this?

24

Use ImageMagick's convert:

convert -resize 128x128 input.svg output.png

If the input canvas size is less than the given size, this will up-scale the output without re-sampling. To get a higher quality result, add -density parameter:

convert -density 500 -resize 128x128 input.svg output.png

ImageMagick rasters vector images to their canvas resolution to a default density of 72 dpi.
To up-scale with proper sampling, you could specify an over high value, like 500 dpi in above sample.

Using a too high raster density can be a performance issue with large size canvas. The lowest and ideal raster density is: raster_density = raster_size / original_size * original_density

This ideal raster density will output image directly to the expected size (no -resize needed). But as float number approximations may occur, a slightly higher value and a resize are recommended for pixel-perfect result.

You can get original size and density using ImageMagick's percent escapes:

 convert input.svg -format "%w %h %[resolution.x] %[resolution.y]" info:

Note that ImageMagick doesn't handle the complete specifications of SVG. If having trouble with shapes rendered incorrectly, look at this thread about selecting ImageMagick SVG renderer.

  • Awesome, thanks. I found rsvg from the answer below on my own, but it's really time I start using ImageMagick. – Naftuli Kay Dec 7 '12 at 17:17
  • 1
    Didn't work for my svg image. At first I received convert: unable to read font (null)' @ error/annotate.c/RenderFreetype/1153.` errors which I resolved by brew install gs, but even when convert ran cleanly the resulting image is garbage. – Lloyd Dewolf Feb 27 '15 at 19:30
  • @LloydDewolf, you can try cleaning the svg first. – Mihai Rotaru Feb 27 '15 at 19:44
  • I had to add -background transparent, otherwise convert uses white background. – hgoebl Nov 22 '15 at 13:18
  • (this applies to linux, may apply to windows) if you turn -verbose on IM then it would appear that IM itself uses inkscape to create an intermediate eps file. therefore i would suggest using inkscape directly with: inkscape in.svg --export-png=out.png (example from superuser.com/a/493028/316154) – northern-bradley Aug 9 '17 at 18:39
9

Both rsvg and inkscape can:

$ rsvg
Usage: rsvg [OPTIONS...] file.svg file.png
  -d, --dpi-x=<float>          pixels per inch
  -p, --dpi-y=<float>          pixels per inch
  -x, --x-zoom=<float>         x zoom factor
  -y, --y-zoom=<float>         y zoom factor
  -w, --width=<int>            width
  -h, --height=<int>           height
  -q, --quality=<int>          JPEG quality
  -f, --format=[png, jpeg]     save format
  -v, --version                show version information

Help options:
  -?, --help                   Show this help message
  --usage                      Display brief usage message

And:

$ inkscape --help
Usage: inkscape [OPTIONS...] [FILE...]

Available options:
  -V, --version                             Print the Inkscape version number
  -z, --without-gui                         Do not use X server (only process
                                            files from console)
  -g, --with-gui                            Try to use X server (even if
                                            $DISPLAY is not set)
  -f, --file=FILENAME                       Open specified document(s) (option
                                            string may be excluded)
  -p, --print=FILENAME                      Print document(s) to specified
                                            output file (use '| program' for
                                            pipe)
  -e, --export-png=FILENAME                 Export document to a PNG file
  -d, --export-dpi=DPI                      The resolution used for exporting
                                            SVG into bitmap (default 90)
  -a, --export-area=x0:y0:x1:y1             Exported area in SVG user units
                                            (default is the canvas; 0,0 is
                                            lower-left corner)
  -D, --export-area-drawing                 Exported area is the entire
                                            drawing (not canvas)
  -C, --export-area-canvas                  Exported area is the entire canvas
      --export-area-snap                    Snap the bitmap export area
                                            outwards to the nearest integer
                                            values (in SVG user units)
  -w, --export-width=WIDTH                  The width of exported bitmap in
                                            pixels (overrides export-dpi)
  -h, --export-height=HEIGHT                The height of exported bitmap in
                                            pixels (overrides export-dpi)
  -i, --export-id=ID                        The ID of the object to export
  -j, --export-id-only                      Export just the object with
                                            export-id, hide all others (only
                                            with export-id)
  -t, --export-use-hints                    Use stored filename and DPI hints
                                            when exporting (only with
                                            export-id)
  -b, --export-background=COLOR             Background color of exported
                                            bitmap (any SVG-supported color
                                            string)
  -y, --export-background-opacity=VALUE     Background opacity of exported
                                            bitmap (either 0.0 to 1.0, or 1 to
                                            255)
  -l, --export-plain-svg=FILENAME           Export document to plain SVG file
                                            (no sodipodi or inkscape
                                            namespaces)
  -P, --export-ps=FILENAME                  Export document to a PS file
  -E, --export-eps=FILENAME                 Export document to an EPS file
  -A, --export-pdf=FILENAME                 Export document to a PDF file
  -T, --export-text-to-path                 Convert text object to paths on
                                            export (EPS)
  -F, --export-embed-fonts                  Embed fonts on export (Type 1
                                            only) (EPS)
  -B, --export-bbox-page                    Export files with the bounding box
                                            set to the page size (EPS)
  -X, --query-x                             Query the X coordinate of the
                                            drawing or, if specified, of the
                                            object with --query-id
  -Y, --query-y                             Query the Y coordinate of the
                                            drawing or, if specified, of the
                                            object with --query-id
  -W, --query-width                         Query the width of the drawing or,
                                            if specified, of the object with
                                            --query-id
  -H, --query-height                        Query the height of the drawing
                                            or, if specified, of the object
                                            with --query-id
  -S, --query-all                           List id,x,y,w,h for all objects
  -I, --query-id=ID                         The ID of the object whose
                                            dimensions are queried
  -x, --extension-directory                 Print out the extension directory
                                            and exit
      --vacuum-defs                         Remove unused definitions from the
                                            defs section(s) of the document
      --verb-list                           List the IDs of all the verbs in
                                            Inkscape
      --verb=VERB-ID                        Verb to call when Inkscape opens.
      --select=OBJECT-ID                    Object ID to select when Inkscape
                                            opens.

Help options:
  -?, --help                                Show this help message
      --usage                               Display brief usage message
  • I was having endless problems with Imagemagick. Had no idea I could use rsvg directly (binary was called rsvg-convert in my case on Mac Homebrew) and this is working fantastically. Thanks. – tremby May 22 '15 at 0:59
  • Using inkscape circumvented a problem I was having with imagemagick -- the SVG -> PNG conversion using imagemagick cropped out the invisible bounding box for the image (used for preserving centering and relative dimensions) but inkscape worked alright. Many thanks! – typesanitizer Nov 24 '15 at 13:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.