zaitsman
zaitsman

Reputation: 9499

Convert svg to png imagemagick black border around path

I am on macOs Mojave 10.14.3 (18D109)

My imagemagick details (installed with brew)

convert -version
Version: ImageMagick 7.0.8-35 Q16 x86_64 2019-03-25 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP 
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib

I have a basic svg (adopted from W3Schools example):

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="52" height="52" viewBox="0 0 52 52">
  <path fill="#fff" d="M22 0 L33 52 L12 42 Z">
</path>
</svg>

That looks like this (Gapplin):

Gapplin view

Now when i convert it to png like so:

convert -background none -density 9600 -resize 32x "myFile.svg" "myFile.png"

i get a perfect image, except it has a black border:

after conversion

I tried just about every variation of command options for imagemagick that had to do with color or black and sadly it never comes out with what i want - transparent background, solid white path (as described by fill on the SVG).

Any suggestions?

Upvotes: 4

Views: 4491

Answers (4)

Bobby Solo
Bobby Solo

Reputation: 53

I know this is an old question, I've tried GraphicsMagic and also with ImageMagick enabled as true. The solution that works for me in getting the best Conversion from SVG to PNG without Black Outlines is using SHARP and Node JS.

const sharp = require('sharp');

function convertSvgToPng(inputPath, outputPath) {
  sharp(inputPath)
    .png() // Convert to PNG format
    .toFile(outputPath, (err, info) => {
      if (err) {
        console.error('Error during conversion:', err);
      } else {
        console.log('Conversion successful!', info);
      }
    });
}

// Example usage
const inputSvg = 'input.svg';
const outputPng = 'output.png';

convertSvgToPng(inputSvg, outputPng);

Upvotes: 0

Fanvaron
Fanvaron

Reputation: 156

In my case, switching converters did not help. Thus I experimented with the SVG itself.

And adding stroke-opacity="0" to the svg (or the individual paths if you dont want everything to have no outline) helped.

I assume that there is some default for stroke values that the converters set that makes them visible and this defines them as invisible on the svg level

Upvotes: 0

Dave Child
Dave Child

Reputation: 7881

I had this problem converting SVG to PNG with PHP - black lines around the path. I install Inkscape on the server that was processing the image and that seemed to fix it, no code change necessary.

Upvotes: 0

fmw42
fmw42

Reputation: 53081

In ImageMagick, SVG files can be rendered by any one of three tools: Imagemagick's MSVG/XML, RSVG delegate and Inkscape, usually in order of increasing accuracy.

I just tried the following command with Inkscape 0.92.4 and it comes out totally white. If Inkscape is installed Imagemagick will use it automatically. But in this case it is not working well with that file.

magick test.svg test2.png


But it works fine if I added -background none

magick -background none test.svg test2.png


I get a white triangle on a transparent background.

or if I want a black background

magick -background black test.svg test2.png


When I force Imagemagick to use MSVG, I get a white triangle with a simple black outline on a white background. So again not correct by your comments.

magick MSVG:test.svg test2.png


If I add -background none, then I get white triangle with black outline on a transparent background.

magick -background none MSVG:test.svg test2.png


But if I just want a black background, then

magick -background black MSVG:test.svg test2.png


I then get a white triangle on a black background. But I suspect the white triangle is slightly smaller from the black border it had.

When I force the use of RSVG 2.44.12 by creating an entry in the delegates.xml file, I get a white triangle on a transparent background, which I assume is what you want.

magick RSVG:test.svg test3.png


or

magick -background none RSVG:test.svg test3.png


You can tell if your Imagemagick has MSVG/XML or RSVG by

magick -list format


and look for the line starting with SVG. At the end of that line it will either say XML or RSVG. You cannot tell about Inkscape with this method.

Check to see what you are using for your SVG renderer and its version. Also the version of libpng. Mine is 1.6.36

From your version information, I do not see rsvp listed. So I assume you are using MSVG, but it could be Inkscape if you have installed that on your system.

I am on Mac OSX Sierra with Imagemagick 7.0.8.35 Q16.

Upvotes: 3

Related Questions