Converting SVG to PNG without interpolating colors[Resolved]

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
uigrad
Posts: 6
Joined: 2014-10-28T09:43:41-07:00
Authentication code: 6789

Converting SVG to PNG without interpolating colors[Resolved]

Post by uigrad » 2014-10-28T09:56:11-07:00

Version: ImageMagick 6.8.9-8 Q16 amd64 2014-10-21 (Unix)

I have simple SVG images with very few colors, and want to convert them to PNGs with the same number of colors. Every attempt that I make will either interpolate (and make new colors), or badly distort the colors. Here's some things I've tried:

Code: Select all

url=http://upload.wikimedia.org/wikipedia/commons/9/9d/FedEx_Express.svg 
convert $url fedex00.png
convert -colors 3 $url fedex01.png
convert -colors 3 -resize 50% $url fedex02.png
convert -colors 3 -resize 200% $url fedex03.png
convert -resize 200% -colors 3 $url fedex04.png
convert -colors 3 -resize 200% -colors 3 $url fedex05.png
for i in fedex*.png; do identify -format %k $i; echo " $i";done
Output:

Code: Select all

34 fedex00.png
3 fedex01.png
381 fedex02.png
2259 fedex03.png
3 fedex04.png
3 fedex05.png
Of all those images, only the first (which has 34 colors) looks right:
Image
Last edited by uigrad on 2014-10-28T13:22:09-07:00, edited 3 times in total.

User avatar
fmw42
Posts: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Converting SVG to PNG without interpolating colors

Post by fmw42 » 2014-10-28T10:07:58-07:00

This works perfectly fine for me on IM 6.8.9.9 Q16 Mac OSX with RSVG delegate. It looks just like the original svg file.

Code: Select all

convert FedEx_Express.svg FedEx_Express.png
What delegate are you using for processing of SVG files? You should use either Inkscape or RSVG. If you are using the IM internal MSVG (XML), that may be the problem. It is much less accurate than the other two.

Check

convert -list format

and see what it says for SVG. Mine is

SVG SVG rw+ Scalable Vector Graphics (RSVG 2.40.2)
SVGZ SVG rw+ Compressed Scalable Vector Graphics (RSVG 2.40.2)


-colors will change colors. Also I believe with the use of -colors and -resize, the input svg file should come right after convert and before such operations.

Any color changes in the simple command above, will likely be due to the delegate library. So use the best one you can. IM relies on the delegate library to deal with SVG, so it is not likely an IM issue.

This also seems to work for me with 8 colors:

Code: Select all

convert FedEx_Express.svg +dither -colors 8 PNG8:FedEx_Express_8c.png
Colors: 8
Histogram:
459: ( 42, 0,124) #2A007C srgb(42,0,124)
137: ( 94, 62,155) #5E3E9B srgb(94,62,155)
31: (194,130,137) #C28289 srgb(194,130,137)
98: (206,176,194) #CEB0C2 srgb(206,176,194)
120: (226,211,224) #E2D3E0 srgb(226,211,224)
287: (255, 89, 0) #FF5900 srgb(255,89,0)
47: (255,114, 38) #FF7226 srgb(255,114,38)
1371: (255,255,255) #FFFFFF white

snibgo
Posts: 9380
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting SVG to PNG without interpolating colors

Post by snibgo » 2014-10-28T10:48:23-07:00

Introduced colours can come from antialiasing. There seems to be no way of turning off Inkscape's anti-aliasing in batch. But it can be done with MSVG:

Code: Select all

convert +antialias MSVG:FedEx_Express.svg f1.png

Code: Select all

convert +antialias -density 500 MSVG:FedEx_Express.svg f2.png
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Converting SVG to PNG without interpolating colors

Post by fmw42 » 2014-10-28T11:31:49-07:00

I can confirm snibgo results using MSVG, but not RSVG.

This works for me also on my Mac OSX and I get your 3 proper colors, though the image is great quality due to the limited number of colors

Code: Select all

convert +antialias MSVG:FedEx_Express.svg f1.png
But it does not work with RSVG either

Code: Select all

convert +antialias RSVG:FedEx_Express.svg f1.png
I get many colors, which looks much better than keeping only the 3 colors.

uigrad
Posts: 6
Joined: 2014-10-28T09:43:41-07:00
Authentication code: 6789

Re: Converting SVG to PNG without interpolating colors

Post by uigrad » 2014-10-28T12:51:06-07:00

fmw42 wrote: What delegate are you using for processing of SVG files? You should use either Inkscape or RSVG. If you are using the IM internal MSVG (XML), that may be the problem. It is much less accurate than the other two.

Check

convert -list format

Code: Select all

% convert -list format | grep SVG
     MSVG  SVG       rw+   ImageMagick's own SVG internal renderer
      SVG  SVG       rw+   Scalable Vector Graphics (RSVG 2.36.4)
     SVGZ  SVG       rw+   Compressed Scalable Vector Graphics (RSVG 2.36.4)
snibgo wrote:Introduced colours can come from antialiasing. There seems to be no way of turning off Inkscape's anti-aliasing in batch. But it can be done with MSVG:

Code: Select all

convert +antialias MSVG:FedEx_Express.svg f1.png
This appears to be exactly what I wanted. Thank you so much!! This (and the version you gave for with -density) work perfectly.

Thanks to everyone that helped.

Post Reply