Reputation: 331
I have been stuck in this problem for some time now. Essentially, I have a bunch of svg images. Each 'child' in the svg file has been labelled with some pixel value. Currently the number of this values is very small and everything is labelled as rgb(0.0, 0.0, 0.0), rgb(1.0, 1.0, 1.0) ... rgb(9.0, 9.0, 9.0), so essentially I have 10 different types of pixels.
Now, I want to convert these images into png format, and more importantly I need the mapping of pixel values to be 1-to-1. Essentially, all pixels that have values rgb(0.0, 0.0, 0.0) in svg files, need to have values rgb(x,x,x) in png files (or even better L(x)); rgb(1.0, 1.0, 1.0) on svg files need to be converted to rgb(y,y,y) on png files (or even better L(y)) and so on. This one to one mapping is a dealbreaker for my application, because this is essentially the ground truth for my work.
By simply writing in the console:
convert test.svg test.png
doesn't give me what I want. Checking the historgram of values, it seems that I have 248 unique values instead of 10, and that isn't good for me (despite that the vast majority of them have just a few pixels).
Does anyone know:
I've tried so far using other libraries like Python's cairosvg but that seems to work even worse. Yes, I know that svg and png are totally different formats.
For clarification, adding an svg and a png file:
svg: https://drive.google.com/open?id=0B_vhcDz1zxeYeGVDSnhfeWplOWs
png: https://drive.google.com/open?id=0B_vhcDz1zxeYUnYzZUtIUmVqVWM
Opening the file in Python, seems that there are 248 unique pixel values, while there should be only 4 (background + three symbols).
Thanks!
Upvotes: 1
Views: 1517
Reputation: 11
3 Years but no great solution gg:
I found 2 good ones:
in the ImageMagick command line you can use:
"convert in.svg +antialias out.png"
but be careful my source said that "+" deactivates AA and "-" activates it. But the source is pretty old and this seems strange so. I couldn't try this
"CAIRO_ANTIALIAS_NONE"
works perfectly for meUpvotes: 0
Reputation: 101800
Your request isn't making a lot of sense. As far as I can see, your sample SVG file only has two colours: black and white (rgb(255.0, 255.0, 255.0)
). So where does this 10 colours idea come from?
Also the SVG standard does not specify exactly how vector shapes should be converted to pixels. There will be subtle differences between SVG renderers.
Remember that vector shape edges that pass through the middle of a pixel will produce a grey pixel. This is called anti-aliasing. It is designed to give a smoother look to the edge. And I imagine that is why you are seeing many more pixel values than you expect.
Perhaps what you are saying is that you want a way to disable anti-aliasing? Some conversion programs may have options to do this. Alternatively you can try adding the shape-rendering
attribute to the root <svg>
tag of your file:
<ns0:svg ...(snip)... shape-rendering="crispEdges">
However some SVG conversion programs may not support this attribute. But you can see it working if you try it in most browsers.
The output generated by turning antialiasing off will not look as good. But perhaps for your purposes you don't care about that.
Alternatively, perhaps you are wanting to know how to convert the SVG to a bitmap, whilst limiting the antialiasing to 10 specific levels of grey? Imagemagick lets you do that. I am not an Imagemagick user, but apparently you can tell imagemagick to use a specific colour paletter by passing a palette image using the -map
parameter.
Upvotes: 2