Converting svg to png without changing the pixel values

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Revan
Posts: 3
Joined: 2017-07-16T02:51:01-07:00
Authentication code: 1151

Converting svg to png without changing the pixel values

Post by Revan »

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!
snibgo
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

Post by snibgo »

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.
snibgo's IM pages: im.snibgo.com
Revan
Posts: 3
Joined: 2017-07-16T02:51:01-07:00
Authentication code: 1151

Re: Converting svg to png without changing the pixel values

Post by Revan »

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).
snibgo
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

Post by snibgo »

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:

Code: Select all

convert -background Blue music.svg x.png
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:

Code: Select all

convert -background Blue +antialias MSVG:music.svg x2.png
Gimp colour-picker says the treble clef is exactly 3/255.

Does that help?
snibgo's IM pages: im.snibgo.com
Revan
Posts: 3
Joined: 2017-07-16T02:51:01-07:00
Authentication code: 1151

Re: Converting svg to png without changing the pixel values

Post by Revan »

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:

Code: Select all

convert test.svg +antialias MSVG:test.png 
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

Code: Select all

shape-rendering="crispEdges"
at the beginning of each svg format and then proceeding with:

Code: Select all

convert test.svg +antialias PNG24:test.png 
Everything seems to work fine in a limited number of tests. On a side note, is there a more efficient way of adding

Code: Select all

shape-rendering="crispEdges"
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?
snibgo
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

Post by snibgo »

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.
snibgo's IM pages: im.snibgo.com
Post Reply