[Python] CairoSVG: The SVG size is undefined

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I need to convert a SVG file into a PNG. To do so, I'm using CairoSVG, which works fine. I have however an image causing the CairoSVG package to raise an Exception, and I don't understand why. Note that the problematic image is public (Wikipedia), which is a good thing for sharing the code bellow:

    import cairosvg

    URL = "https://upload.wikimedia.org/wikipedia/commons/3/32/Saltwater_Limpet_Diagram-en.svg"
    OUTPUT_PATH = "Saltwater_Limpet_Diagram-en.png"
    cairosvg.svg2png(url=URL, write_to=OUTPUT_PATH)


    The error I get is raise ValueError('The SVG size is undefined'). But when I open the SVG file, I can see that the width and height are defined in <svg> tag:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Generator: Adobe Illustrator 18.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
    <svg version="1.1" id="Layer_3" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="2569.14px" height="1481.013px" viewBox="0 0 2569.14 1481.013" style="enable-background:new 0 0 2569.14 1481.013;" xml:space="preserve">

    Continue reading...

Compartilhe esta Página