deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
322 stars 79 forks source link

when I transform svg to png, it will draw some black block on pngs randomly, how does it happen? #287

Closed Pean-Mura closed 3 years ago

deeplook commented 3 years ago

Please share some code.

Pean-Mura commented 3 years ago

Please share some code.

0bfb4e39-ef17-4a6b-8c0a-fcd2cfb698ec above is the png I got .below is the origin svg image

                    drawing = svg2rlg(file)
                    renderPM.drawToFile(drawing,out+name+'.png',fmt='PNG')

code just like this, nothing special. but i process 16 imgs continusly. It randomly get the black block on 2 or 3 imgs.

deeplook commented 3 years ago

Please share a complete minimal example.

Pean-Mura commented 3 years ago

Please share a complete minimal example.

m.zip this is the SVG file. you can download and try it. and my whole code is as flowing: (I found the code looks a little confusing here, so I compress it into a zip file test.zip maybe you will try several times to get the problem

deeplook commented 3 years ago

Sorry, but I'm scared of ZIPs. ;) Would you mind putting the SVG on a gist for your GitHub account? (I've just updated the issue template accordingly.)

Pean-Mura commented 3 years ago

Sorry, but I'm scared of ZIPs. ;) Would you mind putting the SVG on a gist for your GitHub account? (I've just updated the issue template accordingly.)

okay, I have done. https://gist.github.com/Pean-Mura/9c04ad0c48ad831684da55eca40a8a81

deeplook commented 3 years ago

I've had a look at your SVG files using the following snippet to generate PNGs for all:

from glob import glob
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM

for fn in glob('*.svg'):
    drawing = svg2rlg(fn)
    out = fn.split('.')[0] + '.png'
    renderPM.drawToFile(drawing, out, fmt='PNG')

I can see artifacts near some letters like B in 1.svg and 2.svg, but they don't look like black squares to me. See attached images. The Chinese (?) letters seem to be ignored in the PNG output, and appear with outlined squares in PDF output. This is likely a font issue.

For the artifacts you describe around some letters... I suggest you minimize some SVG example even more to only one letter causing this effect in order to isolate it more.

1 2

Pean-Mura commented 3 years ago

I've had a look at your SVG files using the following snippet to generate PNGs for all:

from glob import glob
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM

for fn in glob('*.svg'):
    drawing = svg2rlg(fn)
    out = fn.split('.')[0] + '.png'
    renderPM.drawToFile(drawing, out, fmt='PNG')

I can see artifacts near some letters like B in 1.svg and 2.svg, but they don't look like black squares to me. See attached images. The Chinese (?) letters seem to be ignored in the PNG output, and appear with outlined squares in PDF output. This is likely a font issue.

For the artifacts you describe around some letters... I suggest you minimize some SVG example even more to only one letter causing this effect in order to isolate it more.

1 2

Yeah, it's true that chinese characters will be ignored too. Is there anymethod to solve this problem? Or how to use another font?

deeplook commented 3 years ago

You can try using a dedicated font and/or find some clues in https://github.com/deeplook/svglib/issues/89.