Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick to convert SVG to PNG with transparent background

I'm using ImageMagick via command line to convert a simple SVG to PNG with a transparent background, but for some reason it's not working.

I've already tried some of the suggestions noted here (both convert -background none in.svg out.png and convert -background transparent in.svg out.png) but no luck.

Here's the source for the SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
    xmlns="http://www.w3.org/2000/svg"
    width="130" height="130"
    stroke-width="8"
    stroke="#303030" stroke-linejoin="round"
    style="background-color: rgba(255,255,255,0);"
>
<g transform="translate(65,65)">
    <path fill="red" d="M60,0 h-120 a60,60 0 0 1 120,0" />
    <path fill="white" d="M60,0 h-120 a60,60 0 0 0 120,0" />
    <circle fill="white" r="20" />
    <circle stroke-width="2" fill="white" r="10" />
</g>
</svg>

Here's the version output for ImageMagick convert (running on Mac OS X 10.7.3):

calyodelphi@dragonpad:~/pokemon-story $ convert -version
Version: ImageMagick 6.8.9-0 Q16 x86_64 2014-04-22 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib djvu fftw fontconfig freetype gslib jng jpeg lcms ltdl lzma png ps tiff webp x xml zlib

Any ideas on what I'm not doing correct (or at all)?

EDIT

Adding this bit of info from Timo's suggestion: verbose output to see where ImageMagick is delegating the conversion. Looks like it's using inkscape, which I've pretty much confirmed (by actually using inkscape directly myself) does not successfully render a transparent background.

calyodelphi@dragonpad:~/pokemon-story $ convert -verbose -background transparent wiki-logo.svg wiki-logo-trans.png 
"inkscape" "wiki-logo.svg" --export-png="/var/tmp/magick-232uT59InIw0vnO" --export-dpi="90,90" --export-background="rgb(0%,0%,0%)" --export-background-opacity="0" > "/var/tmp/magick-232vUkxcro2TpS3" 2>&1
mvg:/var/tmp/magick-232yXpy3JTgdpkm=>/var/tmp/magick-232yXpy3JTgdpkm MVG 130x130 130x130+0+0 16-bit sRGB 552B 0.010u 0:00.000
wiki-logo.svg SVG 130x130 130x130+0+0 16-bit sRGB 552B 0.000u 0:00.000
wiki-logo.svg=>wiki-logo-trans.png SVG 130x130 130x130+0+0 16-bit sRGB 10.6KB 0.000u 0:00.009

Inkscape version is Inkscape 0.48.2 r9819

TIDE

like image 319
Calyo Delphi Avatar asked Dec 18 '14 02:12

Calyo Delphi


1 Answers

You are doing it correctly, I just tried the two commands that you provided (convert -background none in.svg out.png and convert -background transparent in.svg) and they both produce a nice PNG with transparent background for me.

Here are the two output images:

enter image description here     enter image description here

My version info (on OS X 10.10.1):

Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-23 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib freetype jng jpeg ltdl lzma png xml zlib

You could run the commands with an additional -verbose parameter. This will show you if + which exernal 'delegate' software ImageMagick employes for the SVG processing:

convert -verbose -background none in.svg out.png

Sorry I couldn't be of more help.

EDIT: Here is my verbose output:

➜  /tmp  convert -background transparent -verbose in.svg out2.png
"inkscape" "in.svg" --export-eps="/var/tmp/magick-71670YVLm4mRNHwOZ" --export-dpi="90,90" --export-background="rgb(0%,0%,0%)" --export-background-opacity="0" > "/var/tmp/magick-71670DClhbmEIL8wY" 2>&1
mvg:/var/tmp/magick-71670MHFJb-BHacGd=>/var/tmp/magick-71670MHFJb-BHacGd MVG 130x130 130x130+0+0 16-bit sRGB 552B 0.000u 0:00.000
in.svg MVG 130x130 130x130+0+0 16-bit sRGB 552B 0.000u 0:00.000
in.svg=>out2.png MVG 130x130 130x130+0+0 16-bit sRGB 10.6KB 0.010u 0:00.009
like image 101
Timo D Avatar answered Sep 28 '22 06:09

Timo D