Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick png to svg Image Size

I have successfully converted a PNG to an SVG using the following command:

convert Slide3.png Slide3_converted.svg

The problem is the resulting file is a whopping 380MB!

I tried this version of the command:

convert -size 2000x1200 Slide3.png Slide3_converted_smaller.svg

But no dice. Same size.

like image 299
Guido Anselmi Avatar asked Sep 10 '13 17:09

Guido Anselmi


1 Answers

SVG is a vector image format, so converting a raster image (jpg, png, gif etc.) to svg can be a complex task.

I tried to convert a simple image: white background with a red circle and a blue square using:

convert input.png output.svg

here's a sample from the svg file created by this command:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="800" height="600">
  <circle cx="0" cy="0" r="1" fill="white"/>
  <circle cx="1" cy="0" r="1" fill="white"/>
  ...
  <circle cx="218" cy="151" r="1" fill="rgb(255,247,247)"/>
  <circle cx="219" cy="151" r="1" fill="rgb(255,40,40)"/>
  <circle cx="220" cy="151" r="1" fill="red"/>
  <circle cx="221" cy="151" r="1" fill="rgb(255,127,127)"/>
  <circle cx="222" cy="151" r="1" fill="white"/>
  <circle cx="223" cy="151" r="1" fill="white"/>
  ...
  <circle cx="799" cy="599" r="1" fill="white"/>
</svg>

basically ImageMagick created a 1px radius circle for each pixel, painting it in the correct color. Starting from a 5KB png my output was a 22MB svg, this explains the huge file size you obtained.

According to ImageMagick documentation (see here) this happens because "AutoTrace" (see here) library is missing:

"If the "AutoTrace" library is not installed and compiled into IM, then the SVG output generated will be a huge number of single pixel circles, generating a binary result, rather than a smooth SVG outlined image. Such images are huge by comparision, and often take a very long time to render by by SVG render."

once you have installed AutoTrace library you can try with something like this:

convert autotrace:A.gif  A_traced.png
like image 80
Andrea Avatar answered Nov 11 '22 23:11

Andrea