Command-line application for converting SVG to PNG on Mac OS X

179

74

Are there any command-line programs that can convert an SVG to PNG, that run on Mac OS X?

Edit: Dylan B had a good answer with ImageMagick. For reference, to install ImageMagick with SVG support on Mac OS X using MacPorts, do

port install imagemagick +rsvg

Lorin Hochstein

Posted 2010-04-26T01:53:51.947

Reputation: 3 037

2

I found a way if you have Google Chrome... and no need to install any other things: http://superuser.com/questions/134679/command-line-application-for-converting-svg-to-png-on-mac-os-x/1036841#1036841

– nonopolarity – 2016-02-07T19:06:39.013

https://cairosvg.org/ works for python3 and seems to have no issue. pip3 install cairosvg – JayRizzo – 2019-05-24T20:31:50.280

Answers

251

Or without installing anything:

qlmanage -t -s 1000 -o . picture.svg 

It will produce picture.svg.png that is 1000 pixels wide.

I have tested it only on OS X 10.6.3.

tst

Posted 2010-04-26T01:53:51.947

Reputation: 2 666

One other note is that it did not preserve the transparent background of my image... still quite handy and worked for me otherwise! :) – Drewdavid – 2014-11-03T21:19:12.947

Any way to do this without it adding a white background? – sudo – 2015-06-25T00:19:17.847

7This solution fails to take transparency into account. The tool rsvg-convert in @ahti's answer worked out before for me. – BenR – 2016-05-19T08:30:41.787

1the -s option takes dimensions in the form "-s 1920x1080" so you can specify the precise dimensions you want. It seems like the image is not clipped if you specify full dimensions. – Arkain – 2016-07-12T16:16:05.420

is there any way to tell qlmanage to use a specific density? man revealed nothing. – computingfreak – 2017-08-22T07:10:49.193

4Putting both width and height parameters after the -s option didn't work for me. Still crops it to a square. Very frustrating! – Erik van der Neut – 2017-08-25T07:45:55.007

1transparency doesn't seem to work – theonlygusti – 2018-01-21T13:07:44.313

41Unfortunately this clips images to a square. – Martijn Pieters – 2011-01-30T15:13:36.147

8Ah, qlmanage -t gives the thumbnail used by Quick Look (in Finder, etc). Clever idea. Unfortunately, these thumbnails can be buggy, especially when there's text involved. – ShreevatsaR – 2011-06-06T08:07:26.197

7This produces images with the svg file in the 1st quadrant. Doesn't auto crop. Trying to convert files from noun project - wish this worked. – Alex Cook – 2012-11-12T20:40:33.170

It will also not scale down the image to fit everything within the specified width. – Johan – 2014-02-21T17:48:00.163

115

I found that for me the best tool for the job is rsvg-convert.

It can be found in brew with brew install librsvg and is used like this:

rsvg-convert -h 32 icon.svg > icon-32.png

(This example creates a 32px high png. The width is determined automatically.

Ahti

Posted 2010-04-26T01:53:51.947

Reputation: 1 259

Actually this one worked, while ImageMagick's convert issued errors and failed to covert a complex SVG – Brice – 2015-04-27T10:03:02.350

8This is by far the best solution I've found for OS X Yosemite +1! – Greg Martin – 2015-05-09T15:03:11.713

This failed for some of my SVGs, strangely. The qlmanage answer worked for all of them but put a white background, which I don't want. – sudo – 2015-06-25T00:35:03.520

If some of your SVGs don't work with this (and you are sure the files are not faulty), consider filing a bug with the librsvg project (https://wiki.gnome.org/Projects/LibRsvg)

– Ahti – 2015-06-29T14:10:59.000

This answer worked for me on OS X 10.10, while the imagemagick answer didn't. – mchen.ja – 2015-08-05T16:06:22.153

@sudo Try normalizing the SVGs using hxnormalize from brew install html-xml-utils. – Lenar Hoyt – 2015-12-14T23:56:54.337

The best solution because it uses a very good rasterizer in terms of resolution. – Paul-Sebastian Manole – 2017-05-17T12:21:06.940

And remember automator is your friend if you have to do a whole bunch: ask for finder items, run shell script passing input as argument, then something like PATH=/usr/local/bin:/bin export PATH for filename in "$@" do finalFilename="${filename%.svg}.png" rsvg-convert -h 300 "$filename" > "$finalFilename" done – Zoë Smith – 2017-07-07T16:45:28.493

rsvg-convert has parameters -d (x-dpi) and -p (y-dpi), to allow user to control density during export – computingfreak – 2017-08-22T07:12:39.067

2With rsvg-convert, the resulting .png had the right dimensions, but the image came out all black instead of the original colors. With qlmanage the image is cropped to a square. Still searching for a solution :-( – Erik van der Neut – 2017-08-25T07:47:45.663

Where the SVG has overlapping semi-transparent elements, these are not rendered correctly. FWIW Affinity Photo has the same problem. – Paul Masri-Stone – 2018-06-01T13:42:55.807

Batch converting like this find . -type f -name "*.svg" -exec bash -c 'rsvg-convert -h 800 "$0" > "$0".png' {} \; – MikeiLL – 2019-10-30T18:45:58.347

This is the only one that worked for me on Mavericks and https://commons.wikimedia.org/wiki/File:Windows_logo_-_2012_derivative.svg

– Aron Ahmadia – 2014-06-10T01:03:44.397

45

ImageMagick is an extremely versatile command-line image editor, which would probably rival Photoshop if it had, you know, a GUI. But who needs those anyways. :P

Something like the following would convert a .svg to .png, after installation:

$ convert picture.svg picture.png

The original .svg isn't deleted.

Jessie

Posted 2010-04-26T01:53:51.947

Reputation: 1 718

Even using -density the export is weird. I thought my SVG were of poor quality (all circles were in fact polygons) using rsvg-convert worked much better! – AsTeR – 2014-10-22T13:10:11.943

This is the only one that works for all SVGs for me. The quality seems nice, unless I'm missing something. I used -density 600. – sudo – 2015-06-25T01:12:33.407

It sort of has a GUI, in display. – Ignacio Vazquez-Abrams – 2010-04-26T04:04:31.640

1When I installed ImageMagick with Fink, I couldn't convert svg to png - there were some errors. It turned out that I needed to install librsvg2-bin as well. – tst – 2010-05-21T12:44:47.377

For an 800kb SVG file, imagemagick has already used 15 minutes of CPU time and is still going. This is not ideal. I usually like using ImageMagick. – Dave Cameron – 2019-07-29T16:46:07.243

If librsvg2-bin isn't installed (like on OS X) this will fail. Couldn't find a way to get that installed on OS X. – John Sheehan – 2012-08-03T18:53:54.967

3This won't work well if you want to resize the SVG as it generates blurry images. – Behrang Saeedzadeh – 2013-05-23T03:04:19.867

3It doesn't convert all SVG files correctly either; at least qlmanage got all the parts of the image. – Johan – 2014-02-21T19:50:02.800

28

Inkscape with it's Commandline-Interface produces the best results for me:

/Applications/Inkscape.app/Contents/Resources/bin/inkscape --export-png output.png -w 1024 -h 768 input.svg

Good thing is, you can specify the exact pixel size of the resulting image, without having to mess with density.

seb

Posted 2010-04-26T01:53:51.947

Reputation: 399

This is the only method that rendered my hand-written svg correctly – Griffin – 2014-07-31T23:45:06.897

Convert does not generate good PNG files from SVG. Using Inkscape is the best way I have found so far. – ol_v_er – 2015-02-27T07:43:25.617

This works for me, but creates extremely blurry files. – Marcin – 2015-04-16T13:04:55.057

3I had a problem to find files so run it as inkscape $(pwd)/logo.svg --export-png $(pwd)/logo.png – Andrei – 2015-06-07T08:32:11.643

@Marcin @ol_v_er Add e.g. --export-dpi 600 to get higher quality – Andrei – 2015-06-07T08:45:19.767

this is working for me! - ihave a bigger SVG created with Inkscape – matheszabi – 2013-10-07T21:56:09.697

18

OK, I found a simple way to do it on the Mac if you have Google Chrome.

In one sentence, it is to see the svg image in a webpage (must be in an html file), right click on image and choose "Copy Image" and paste to the Preview app.

Steps:

  1. Download or have the svg file in your hard drive, say, somefile.svg
  2. Now, in the same folder, just make an html file tmp.html that contains this line: <img src="somefile.svg">
  3. Now, open that html file in Google Chrome
  4. You should see the image. Now just right click on the image and choose "Copy Image"
  5. Go to Mac's Preview App, and choose, "File -> New from Clipboard"
  6. Now File -> Save the file and you have the png file. (or other file types).

This is tested on the current Chrome (version 48.0) on Mac OS X El Capitan.

Update: I am not sure whether it is due to some restriction imposed by Google Chrome. I just try an SVG file using Chrome 58.0, and I get a tiny image from the method above. If you see this case too, you can also use

<img src="somefile.svg" style="height: 82vh; margin-top: 9vh; margin-left: 9vh">

or if you want more margin, use:

<img src="somefile.svg" style="height: 62vh; margin-top: 19vh; margin-left: 19vh">

and you will have an image on screen good enough for you to do a screenshot -- using CmdShift4 or CmdShift3 on the Mac, for example. Make sure you resize your Chrome window to the maximum allowed on screen first.

nonopolarity

Posted 2010-04-26T01:53:51.947

Reputation: 7 932

Smart solution! – Manu – 2016-03-02T15:07:14.990

1This actually produces the best results for me since it renders exactly the same as the browser. – Jason Haslam – 2017-01-26T01:25:57.253

the resulting image was very poor resolution for me – Michael – 2017-09-22T16:02:21.030

Ingenious :) but for very large SVGs, the resolution of the resulting image is too low, as @Michael mentioned. – waldyrious – 2018-10-23T10:24:02.510

6

If you want to do many at once, you can:

mogrify -format png *.svg

There are options to resize etc on the fly, too..

DefenestrationDay

Posted 2010-04-26T01:53:51.947

Reputation: 209

-1? Someone thinks this isn't helpful? What's the error? – DefenestrationDay – 2014-07-29T20:41:12.783

2I'm not the one who downvoted, but worth noting is that ImageMagick stupidly converts svg to a raster image of arbitrary size before resizing, resulting in blurry output. – Dae – 2015-11-01T19:47:53.267

Installing inkscape first (and getting it on the path) can improve conversion results. – Holly Cummins – 2019-11-26T17:28:55.153

3mogrify is also par of ImageMagick. – ShreevatsaR – 2011-06-06T08:19:20.177

Works great, but brutally slow. – Meekohi – 2014-01-24T21:58:13.533

4

Try Apache Batik.

java -jar batik-rasterizer.jar FILES

It also supports batch conversion and has many other useful options.

Behrang Saeedzadeh

Posted 2010-04-26T01:53:51.947

Reputation: 1 834

4

I have made svgexport using node/npm for this, it is cross-platform and can be as simple as:

svgexport input.svg output.png

Ali Shakiba

Posted 2010-04-26T01:53:51.947

Reputation: 181

This was the easiest one to use and remember by far. Also, the only one that produced a correct result in my case. – Marko Grešak – 2017-05-09T22:09:54.937

resulting resolution wasn't great for me. – Michael – 2017-09-22T16:22:34.913

3

You can also use phantomjs to render the svg. The advantage is that it renders it like a browser would since it's basically a headless WebKit.

Once you download it you need phantomjs (binary) and the rasterizer.js file from the examples folder.

phantomjs examples/rasterize.js Tiger.svg out.png

Stofke

Posted 2010-04-26T01:53:51.947

Reputation: 156

1For reference, this can be installed with brew cask install phantomjs. In my install, the examples folder was located in the path /usr/local/Caskroom/phantomjs/2.1.1/phantomjs-2.1.1-macosx/examples/. – waldyrious – 2018-10-23T10:26:07.923

3

This is what I used:

brew install imagemagick --with-librsvg

Then run the following commands:

find . -type f -name "*.svg" -exec bash -c 'convert $0 $0.png' {} \; rename 's/svg\.png/png/' *

Hope it helps.

Alan Dong

Posted 2010-04-26T01:53:51.947

Reputation: 173

Would be great with an added explanation for what the command does. – Winterflags – 2020-01-27T19:42:36.127

2

Yet another method without installing anything. Not in command line though.

  1. Open the .svg file in Safari.
  2. Press alt-command-i to open the inspector.
  3. Right-click on the <svg> tag, select "Capture Screenshot". (Note that you mustn't zoom in the image.)

P.S. To enlarge the .svg image if it's too small, try opening the .svg file in text editor and append 0 to every number except in the meta-attribute. This can be done by a global regex substitution from (\d+) to $10, where $1 is the placeholder for back reference, for example.

Haotian Yang

Posted 2010-04-26T01:53:51.947

Reputation: 31

You probably should mention that this is almost the same as  太極者無極而生’s answer.

– Scott – 2019-05-11T19:27:14.650

1

I use this command on my linux. It should work for you as well.

mogrify +antialias -density 2000 -verbose -format png *.svg

I learned that without the "-density" argument, the bitmap would be very pixelized. Change the -density value to match your need.

BersekerBernhard

Posted 2010-04-26T01:53:51.947

Reputation: 19

2Even with density, the conversion does not generate an image that is as sharp as a vector image. Try Apache Batik instead. – Behrang Saeedzadeh – 2013-05-23T03:06:36.707

1

ImageMagick's convert command, using some other parameters, is what did it for me. Here's my batch Bash script solution that divides the task across multiple processes to make use of all your cores! Modify as needed.

batchConvertToSVG.sh (takes number of processes as argument):

end=$(( $1 - 1 ))
for i in `seq 0 $end`;
        do
            echo Spawning helper $i of $end
                ./convertToSvgHelper.sh $i $1 &
        done 

convertToSvgHelper.sh:

n=$1
for file in ./*.svg; do
   filename=${file%.svg}
   echo converting file named $filename
   test $n -eq 0 && convert -format png -resize 74 -background transparent -density 600 $file $filename.png
   n=$((n+1))
   n=$((n%$2))
done

sudo

Posted 2010-04-26T01:53:51.947

Reputation: 549

0

As commented previously ImageMagick does the trick. I just wanted to add a point for GraphicsMagick, an old fork of ImageMagick that has some improvements (and much less dependency bloat when installed via fink).

user31752

Posted 2010-04-26T01:53:51.947

Reputation:

0

You can perform a batch conversion on an entire folder of SVG files to PNG. I used Inkscape command line interface to produce png files with a width of 80px.

find ~/desktop/toconvert '*.svg' -exec /Applications/Inkscape.app/Contents/Resources/bin/inkscape -z -w 80 -e "{}".png "{}" \;

png will be saved with original name *.png

James Matthew Mudgett

Posted 2010-04-26T01:53:51.947

Reputation: 1

-1

wkhtmltoimage (from project wkhtmltopdf) did this convert well:

wkhtmltoimage --zoom 2 foo.svg foo.png

ImageMagick renders CJK character as blank on my mac.

georgexsh

Posted 2010-04-26T01:53:51.947

Reputation: 119

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

– Blackwood – 2018-12-14T04:09:49.217

@Blackwood fine, updated. – georgexsh – 2018-12-14T08:11:12.073