emcconville / wand

The ctypes-based simple ImageMagick binding for Python

Home Page:http://docs.wand-py.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Encoded SVG strings are parsed incorrectly on macOS

pcclarke opened this issue · comments

I'd like to convert a SVG string to a PNG image using Wand. On Linux, this works fine. On macOS, I'm consistently seeing black strokes and other changed styling that shouldn't be there. I have tried the same SVG files with MagickWand, and encountered no issues on either operating system. I describe the issue I'm encountering a little more on Stack Overflow.

What I've noticed is that the SVGs that Wand parses from a string have extra elements. For example, with this script:

from wand.image import Image

testsvg2 = '<svg width=\"10\" height=\"10\"><rect x=\"2\" y=\"2\" width=\"5\" height=\"5\" fill=\"#FFFFFF\"></rect></svg>'
with Image(blob=testsvg2.encode()) as img:
  img.save(filename='svg-blob.svg')
  with img.convert('png') as converted:
    converted.save(filename='png-blob.png')

The PNG image has a black border:
png-blob

And the SVG now has an extra <g> element containing what I think is supposed to be default styling but I would guess it's causing the black border:

<?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="10" height="10">
<g style="fill:black;fill-opacity:1;stroke:none;stroke-width:1;stroke-opacity:1;fill-rule:nonzero;">
<g style="fill:#FFFFFF;">
  <rect x="2" y="2" width="5" height="5"/>
</g>
</g>
</svg>

The question is, why is Wand adding a black border? I'm seeing the same on other, more complex images but I can't understand why even on this simple example the black border is there.

My setup is:

  • macOS Monterey 12.6
  • Python 3.10.7
  • Flask 2.2.2
  • Wand 0.6.10
  • ImageMagick 7.1.0-49 Q16-HDRI x86_64 2047
  • Inkscape 1.2.1