Last year I wrote about how to create a great animated GIF by first creating a video. In fact I used the procedure described there to generate animated GIF banners for some of our website pages.
Another similar problem, whether building a website or creating graphics to go in your videos, is converting SVG files into PNG files. If you just need to make one PNG, you’d probably just fire up a program like Inkscape and convert it by hand.
But what if you need to convert a large number of them? Say you want to change your branding color scheme and import all those new PNGs into your favorite video editor. Updating them by hand could take you all day.
So let’s look at our options for speeding things up.
We used ffmpeg for the video conversion. While it does do some image conversion, I wouldn’t recommend it for that. What are some more realistic possibilities?
I think you can tell we’re already leaning heavily towards ImageMagick here…
Now I’m not going to say ImageMagick is the easiest solution. It has a ton of features, but that also means there’s a really steep learning curve. So I’m hoping that by figuring it out I can at least save you some time and hassle.
So what do we need that an image processor like ImageMagick can help us with?
Obviously ImageMagick can do tons of other things, like rotate at arbitrary angles (-rotate X
), add or subtract borders (-shave X
), etc. But features aren’t the only thing. It turns out that the order of the parameters is vitally important.
Let’s explore my solution.
Let’s put our SVG files in ./svgs/
and let ./pngs/
be our output directory.
The shell command to use is mogrify (parameter explanations at this link), whose most basic syntax is:
mogrify image.svg image.png
We can add a number of options that are useful in our case:
-background <color>
-colors 2
-density 300
-fill <color>
-format <file-type>
-opaque <color>
-path <file-path>
-resize 256x256
It’s also nice that for colors we can use basically whatever a browser will take: #RGB, #RGBA, named colors, etc., and we can use none
to mean transparent
(default is a white background).
To switch from the SVG’s path/stroke color, you need to know how the “fill” and “opaque” options work. “opaque” refers to the color in the SVG, “fill” means the color you want to replace that with in the PNG.
Don’t try to use the antialising options, you’ll just get color bleed around the edges of your foreground color. Use the “density” and “resize” options instead. If you do it right you can get extremely lovely 1000×1000 PNGs at only 20KB.
A quick reminder that the order of the options is very important, because they’re treated almost as pipeline operators. Experiment with the order if you want to obtain some unexpected but likely useless results.
Here’s what you’ll probably want to use, though (assuming your SVGs are monochrome white foreground):
mogrify -path .\pngs -format png -fill red -opaque white -background none -colors 2 -density 300 .\svgs*.svg
The result with a basic approach using ImageMagick is quite satisfactory. In seconds I ended up with exactly what I wanted, all my SVGs converted to transparent PNGs in the color I wanted, in the directory I wanted.
Now whenever I need to convert SVGs, I can just paste this one line, change the details, and be done with it. No need to find a new web page convertor, or maintain a script.
If it ever needs to be improved, I would say getting it to work for multiple colors, or keeping up to date with changes to the SVG spec would probably be the things to look at (I had one SVG break because it was entirely an embedded BASE64 image).
Did you like this article? Does it reflect your skills? Würth Phoenix is always looking for talented, enthusiastic individuals to help us drive our business. In fact, we’re currently hiring for many different roles here at Würth Phoenix.