简体   繁体   中英

Why am I getting ValueError when trying to render PNG?

I have some SVGs that I would like to save as PNGs with Python. I am using svglib to handle this process. Whenever I try to renderPM.drawToFile(logo, path, fmt) it returns the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Myself\Desktop\programming\project-folders\svg-to-png\venv\lib\site-packages\reportlab\graphics\renderPM.py", line 683, in drawToFile
    c.saveToFile(fn,fmt)
  File "C:\Users\Myself\Desktop\programming\project-folders\svg-to-png\venv\lib\site-packages\reportlab\graphics\renderPM.py", line 321, in saveToFile
    im = self.toPIL()
  File "C:\Users\Myself\Desktop\programming\project-folders\svg-to-png\venv\lib\site-packages\reportlab\graphics\renderPM.py", line 317, in toPIL
    (getattr(im,'frombytes',None) or getattr(im,'fromstring'))(self._gs.pixBuf)
  File "C:\Users\Myself\Desktop\programming\project-folders\svg-to-png\venv\lib\site-packages\PIL\Image.py", line 827, in frombytes
    d.setimage(self.im)
ValueError: tile cannot extend outside image

Here is the code I entered in the python shell to get these results (see documentation) :

>>> from svglib.svglib import svg2rlg
>>> from reportlab.graphics import renderPM
>>> logo = svg2rlg(r'C:\Users\Myself\Desktop\testenv\test.svg')
>>> renderPM.drawToFile(logo, r'C:\Users\Myself\Desktop\testenv\test.png', fmt='PNG') 

Here is the SVG that I am trying to export as PNG (using a very simple SVG for testing):

<!DOCTYPE html>
<html>
  <body>
    <svg xmlns="http://www.w3.org/2000/svg" height="500" width="500">
      <polygon points="200,10 250,190 160,210" style="fill:lime;stroke:purple;stroke-width:1" />
    </svg>
  </body>
</html>

I have looked at many similar problems to try to find an answer, but I am not having any luck.

As mentioned by @Robert Longson , the HTML file was giving me the value error. I changed the document to include the proper format, which fixed my issue:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM