Code Self Study Forum

How to bulk convert SVG to PNG in the terminal

I just learned that Inkscape can convert from SVG to PNG at the command line:

$ inkscape -z -e filename.png -w 300 -h 300 filename

This will bulk-convert an entire directory of SVG images to PNG:

for f in *.svg; do
   inkscape -z -e ${f:0:-4}.png -w 300 "$f"
done

(${f:0:-4}.png strips off the .svg extension and adds .png)

1 Like

At first I thought this sounded like something ImageMagick could do as well, but after poking around a bit I saw where people have had trouble with this particular conversion in ImageMagick. The suggested work-around is often to just to use Inkscape instead.

The conversion worked well for most of the images, but after looking through more of them, I saw that there was a problem with a few of them, even when opening them in Inkscape’s GUI. I’m not sure why it wasn’t rendering them correctly. Firefox and KolourPaint were rendering them. I wonder if the SVG images were using an SVG tag or feature that isn’t supported in Inkscape.

Edit: I don’t see anything unusual in the SVG source (the values were removed below to reduce the amount of text).

<svg xmlns="http://www.w3.org/2000/svg" width="" height="" viewBox="">
    <g transform="matrix()">
        <rect y="" x="" height="" width="" rx="" ry="" fill="#fff" stroke="#000" stroke-width="" />
        <path d="" />
    </g>
    <path d="" fill="" />
    <path d="" fill="" />
    <path d="" fill="" />
    <path d="" />
    <path d="" />
</svg>

Interesting.