6

The question is simple, but I have googled a lot of methods, and there no such solution as:

import svg-render-library
figure = svg-render-library.open('test.svg')
figure.render()

Is there any simple methods to display an SVG image using only python libraries? I am asking about rendering the SVG image without any conversion to another formats and to render using pure python, without any 3rd party software. As I have tried, this seems impossible for now.

As built-in python I mean - only python packages available through pip, so it is not necessary to install/compile anything else. And to render I mean to show inside window which part of the python, not the browser or any external software.

XuMuK
  • 564
  • 4
  • 11
  • 32
  • Does this answer your question? [Convert SVG to PNG with Python on Windows](https://stackoverflow.com/questions/65759849/convert-svg-to-png-with-python-on-windows) – s d Mar 27 '23 at 20:58
  • also check this one https://stackoverflow.com/a/74164750/4002319 for scaling – s d Mar 28 '23 at 03:16
  • @sd, the convertation SVG-> other formats is not the problem, I want to render exactly an SVG image using only python modules. – XuMuK Mar 28 '23 at 16:24
  • 1
    I've had no success doing this in the past. A lot of tooling require system libraries to perform the action, and I haven't found anything pure python. Also, I've only had good success using tools that perform the rendering with headless browsers. The overall quality of other tooling create terrible looking images. +1 to the question. Hope you find a decent solution. – flakes Mar 28 '23 at 23:02
  • There's a SVG parser at https://github.com/cjlano/svg - and there is a SVG renderer at https://pypi.org/project/drawsvg/ - have you tried these? – brennanyoung Mar 29 '23 at 12:17
  • Do you mean in-built python libraries/modules? – Kayvan Shah Mar 31 '23 at 06:44
  • As already said above, pay attention to the display quality of possible solutions, many tools around are not able to display correctly all SVG features because they allow only subsets of them. I learnt it the hard way and finally decided to use a browser, which is one of the most reliable and ever-present way to display SVG. Hope you find a better solution. – mmj Apr 01 '23 at 09:00

1 Answers1

1

Currently, there is no method to render natively cross-platform with just the standard library (ie. some python distributions for OSX do not include tkinter by default). Ergo, there is no good way to do this.

AFAIK, there are no other ways to do this maintaining your described API without writing your own code or reaching out to non-standard library modules.

If you still are 100% set on doing it with pure python and the standard library, you have tkinter, and don't care about writing your own implementation, then proceed.

If you are talking about rendering in the context of displaying an SVG in a window, then your best bet would be to utilize the tkinter and xml modules.

SVG is just an XML file, so xml.minidom should be able to parse an svg file. You can then extract the height and width from the dom, draw each element onto a tkinter.Canvas widget, and then have tkinter render the window. You will need to either pre-calculate transparencies or handle that while managing the layering.

Another option is to use the turtle package which wraps around tkinter. If the SVG is just a path, then this should be able to draw that path fairly straight forward.

If you are willing to reach out beyond the standard library, then cairosvg or svglib both can easily handle this task. cairosvg is a bit of a bear if you aren't used to installing system libraries, but svglib is a pure python implementation.

plunker
  • 1,190
  • 2
  • 14
  • So, it is possible to use only python modules to render svg files. Also the `PyQt5` can render it. – XuMuK Apr 04 '23 at 16:08
  • XuMuk, PyQt5 is not "built-in" to python and is an external dependency meaning that no, it is not possible to render an SVG with just python "built-in" modules in a cross-platform manner. If you reach out then sure PyQT5 and svglib or cairosvg can do it easily. – plunker Apr 04 '23 at 18:48
  • I fully admit, that my formulations maybe not precise, how to formulate `the only necessary packages can be installed using pip without any additional installations/compilations`. Because cairo requires installation/compilation. For PyQt5 I just used pip install. – XuMuK Apr 05 '23 at 13:41