Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert and resize SVG to PNG

I am trying to convert SVG to PNG (or any raster format) and resize at the same time.

I thought I would use ImageMagick for this task but it seems to be converting to raster before resizing.

This results in a poor quality image.

  • Is there a way to get ImageMagick to resize the SVG before converting to raster?

  • Or is there some other tool I can use to programatically convert an SVG to a raster after resizing it?

  • Alternatively, is there some other tool I could use for this?

Currently I'm using ImageMagick via a commandline:

convert file.svg -resize 100x100 file.png 

The source image "size" is unknown and the destination size is not known until run-time.

like image 464
DJL Avatar asked Aug 12 '13 16:08

DJL


People also ask

Is SVG format better than PNG?

SVGs offer lossless compression — which means they're compressible to smaller file sizes at no cost to their definition, detail, or quality. PNGs also benefit from lossless compression of 5-20%, which can help make up for their large file size. However, they're still likely to be larger than an SVG.

Is SVG clearer than PNG?

PNGs and SVGs support transparency — so they're both excellent choices for online logos and graphics. It's worth noting that PNGs are one of the best choices for a raster-based transparent file. If you're working with pixels and transparency, PNGs are a better option than SVGs.


2 Answers

This is the way I do it and it seems to work.

convert -background none -density 1000 -resize 1000x compass.svg compass.png 

Here's what each part does.

  • Use -background none to make sure any transparent parts of the SVG stay transparent and not get filled with white.
  • As ImageMagick only works with raster images you need to use -density 1000 and specify the width to which you want to resize the SVG. This way when you actually call the resize command, the raster representation of the loaded SVG will already have a width of 1000, otherwise you'll end up resizing a raster up or down from whatever the original size of the SVG image is.
  • Now use -resize 1000x to give your SVG a new width, the height will be calculated automatically to keep the aspect ratio.

One pitfall of this, is that I don't really know how you could resize based on the height and let the width be calcualted since -density is applied to the width, not the height. So, you'd have to know the actual ratio of your SVG beforehand and work with the width accordingly.

like image 164
Tony Bogdanov Avatar answered Sep 21 '22 02:09

Tony Bogdanov


The SVG is defined as a vector, not a bitmap - which is what IM likes dealing with. When IM reads a vector in, it doesn't know the size, so when you do this:

convert compass.svg -resize 1000x1000 compassB.jpg 

it creates a default sized bitmap canvas, "draws" the vector onto it, then resizes it and saves it as a JPEG. The result, if your intended size is larger than the canvas that IM "guessed" is poor quality - it cannot create information it no longer has as a result of rasterizing the image onto too small a canvas.

enter image description here

The solution is to tell IM up-front that the vector needs to be drawn onto a big canvas, before it rasterises:

convert -size 1000x1000 compass.svg compassA.jpg 

enter image description here

like image 22
Mark Setchell Avatar answered Sep 22 '22 02:09

Mark Setchell