Hi guys,
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:
- if this can be done.
- how this can be done.
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.
Thanks!
Converting svg to png without changing the pixel values
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Converting svg to png without changing the pixel values
Please provide a sample SVG. Please also state your IM version number and platform.
SVG doesn't have pixels. It does have areas such as rectangles. When these are rendered, there may be anti-aliasing.
SVG doesn't have pixels. It does have areas such as rectangles. When these are rendered, there may be anti-aliasing.
snibgo's IM pages: im.snibgo.com
Re: Converting svg to png without changing the pixel values
Fair point, I uploaded an SVG file and the corresponding PNG file.
svg: https://drive.google.com/open?id=0B_vhc ... nhfeWplOWs
png: https://drive.google.com/open?id=0B_vhc ... UtIUmVqVWM
My version of ImageMagick is: ImageMagick 6.8.9-9 Q16 x86_64 2017-05-26
Platforum: Linux Ubuntu 16.04
Is there a way to disable anti-aliasing? NB: tried the +antialias option, but it doesn't change anything.
Opening the file in Python, seems that there are 248 unique pixel values, while there should be only 4 (background + three symbols).
svg: https://drive.google.com/open?id=0B_vhc ... nhfeWplOWs
png: https://drive.google.com/open?id=0B_vhc ... UtIUmVqVWM
My version of ImageMagick is: ImageMagick 6.8.9-9 Q16 x86_64 2017-05-26
Platforum: Linux Ubuntu 16.04
Is there a way to disable anti-aliasing? NB: tried the +antialias option, but it doesn't change anything.
Opening the file in Python, seems that there are 248 unique pixel values, while there should be only 4 (background + three symbols).
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Converting svg to png without changing the pixel values
The sample SVG you have uploaded contains musical notations. Most are "rgb(255.0, 255.0, 255.0)", ie white, which don't show well on a white background. The treble clef is "rgb(3.0, 3.0, 3.0)". To make them all visible, I can do:
Now we have white marks and nearly black marks on a blue background. When I zoom in, I can see anti-aliasing: edges of the marks change gradually to the background.
The method to turn off anti-aliasing depends on the delegate. With MSVG:
Gimp colour-picker says the treble clef is exactly 3/255.
Does that help?
Code: Select all
convert -background Blue music.svg x.png
The method to turn off anti-aliasing depends on the delegate. With MSVG:
Code: Select all
convert -background Blue +antialias MSVG:music.svg x2.png
Does that help?
snibgo's IM pages: im.snibgo.com
Re: Converting svg to png without changing the pixel values
Not showing well on a white background was exactly the point. I want to hide everything, bar the symbols I want.
I don't know anything about delegate and MSVG. Ignoring the -background part (because I do not need that), simply writing:
Where test is an svg image, gives me an png file which I cannot open.from Python (or from any ImageViewer or a Browser). Not opening it from Python makes the entire thing unusable for me. Do you know if I can do anything about it?
I found a workaround around this, by adding
at the beginning of each svg format and then proceeding with:
Everything seems to work fine in a limited number of tests. On a side note, is there a more efficient way of adding
for each file than opening each file in my dataset and adding that line? I thought that +antialias command should do the same as shape-rendering="crispEdges".
Also, are there any cons on using crispEdges bar some decrease on quality?
I don't know anything about delegate and MSVG. Ignoring the -background part (because I do not need that), simply writing:
Code: Select all
convert test.svg +antialias MSVG:test.png
I found a workaround around this, by adding
Code: Select all
shape-rendering="crispEdges"
Code: Select all
convert test.svg +antialias PNG24:test.png
Code: Select all
shape-rendering="crispEdges"
Also, are there any cons on using crispEdges bar some decrease on quality?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Converting svg to png without changing the pixel values
You put the "MSVG:" in the wrong place.
"shape-rendering" is a hint, to be included within the SVG file. Perhaps it would be a good idea for IM, when "+antialias" is in effect, to add this to the SVG file before passing to the renderer.
"shape-rendering" is a hint, to be included within the SVG file. Perhaps it would be a good idea for IM, when "+antialias" is in effect, to add this to the SVG file before passing to the renderer.
snibgo's IM pages: im.snibgo.com