Last active
April 27, 2023 20:47
-
-
Save villares/c5d097b8a8c6178284ae0a6adfd3e7e5 to your computer and use it in GitHub Desktop.
Showing SVG with PySimpleGUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from io import BytesIO | |
| import PySimpleGUI as sg | |
| import cairosvg | |
| # This didn't work with the example file in this gist (the raster background didn't show up) | |
| svg_content = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect x="10" y="10" width="80" height="80"/></svg>' | |
| # filename = 'NdPAbril2.svg' | |
| # with open(filename, 'r') as f: | |
| # svg_content = f.read() | |
| png_bytes = BytesIO() | |
| cairosvg.svg2png(bytestring=svg_content, write_to=png_bytes) | |
| layout = [[sg.Image(data=png_bytes.getvalue())]] | |
| window = sg.Window('SVG Viewer', layout) | |
| while True: | |
| event, values = window.read() | |
| if event == sg.WIN_CLOSED: | |
| break | |
| window.close() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from io import BytesIO | |
| import PySimpleGUI as sg | |
| from svglib.svglib import svg2rlg | |
| from reportlab.graphics import renderPM | |
| filename = 'NdPAbril2.svg' # | |
| drawing = svg2rlg(filename) | |
| io_bytes = BytesIO() | |
| renderPM.drawToFile(drawing, io_bytes, fmt="PNG") | |
| png_bytes = io_bytes.getvalue() | |
| layout = [[sg.Image(png_bytes)]] | |
| window = sg.Window('SVG Viewer', layout) | |
| while True: | |
| event, values = window.read() | |
| if event == sg.WIN_CLOSED: | |
| break | |
| window.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment