SVG to black-and-white


Question

I would like to be able to convert SVG documents to black and white. My try is the following Makefile script using 'sed' :

%.bw.svg: %.svg
    sed '/stroke:none/!s/stroke:[^;\"]*/stroke:black/g' $< > $@

This works for lines etc but not for fillings. Basically if the stroke is not invisible (none), then I convert it to black. I would like to do the same for fillings, if not white or invisible, then convert to black.

I wonder if it would be too complex to do something like this in a better way, perhaps using XSLT, but I have no experience. Anyone can help ?

1
2
2/5/2010 11:40:20 AM

Accepted Answer

Two options that I would try:

1- Inkscape appears to be able to do it - Inkscape Convert
2- SVG supports a ColorProfile attribute on the SVG element that can reference an ICC Color Profile. I would try to reference a GrayScale color profile there and see what happens. Looks like there is one available here.

3
2/4/2010 9:30:18 PM

First of all: Don't try doing this with sed. Editing XML is a little more complex than that.

You can use an SVG filter effect on the image. The ColorMatrix filter primitive can desaturate an image.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon