Learn how to Convert SVG Code to PNG in Python using Aspose.SVG
Last active
October 22, 2025 08:03
-
-
Save aspose-com-gists/94e00c65027a5dba498ee87196aabaee to your computer and use it in GitHub Desktop.
Convert SVG Code to PNG in Python using Aspose.SVG
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
| import aspose.svg as svg | |
| from aspose.svg.rendering.image import ImageDevice, ImageFormat, ImageRenderingOptions | |
| # SVG code as a string | |
| svg_code = """ | |
| <svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"> | |
| <rect width="200" height="200" fill="lightblue"/> | |
| <circle cx="100" cy="100" r="80" fill="orange" stroke="black" stroke-width="2"/> | |
| <text x="100" y="110" text-anchor="middle" font-size="24" fill="white">SVG</text> | |
| </svg> | |
| """ | |
| # 1. Load from string + base URI (use "." if you don't rely on external assets) | |
| document = svg.SVGDocument(svg_code, ".") | |
| # 2. Pick PNG as output | |
| options = ImageRenderingOptions() | |
| options.format = ImageFormat.PNG | |
| # 3. Create a device that writes the rendered output to a file | |
| device = ImageDevice(options, r"source_out.png") | |
| # 4. Render SVG to PNG | |
| document.render_to(device) |
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
| import aspose.svg as svg | |
| from aspose.svg.rendering.image import ImageDevice, ImageFormat, ImageRenderingOptions | |
| # Load SVG file | |
| document = svg.SVGDocument("aspose-svg-logo.svg") | |
| # Pick PNG as output | |
| options = ImageRenderingOptions() | |
| options.format = ImageFormat.PNG | |
| # Initialize PNG rendering device | |
| device = ImageDevice(options, "aspose-svg-logo.png") | |
| # Render and save the output | |
| document.render_to(device) |
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
| import aspose.svg as svg | |
| from aspose.svg.rendering.image import ImageDevice, ImageFormat | |
| import io | |
| svg_code = """ | |
| <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"> | |
| <rect width="100" height="100" fill="green"/> | |
| </svg> | |
| """ | |
| # Create SVG document from string | |
| document = svg.SVGDocument(svg_code, ".") | |
| # Choose PNG format | |
| opts = ImageRenderingOptions() | |
| opts.format = ImageFormat.PNG | |
| # Render to in-memory stream | |
| memory_stream = io.BytesIO() | |
| device = ImageDevice(opts, memory_stream) | |
| document.render_to(device) | |
| # Access PNG bytes | |
| png_data = memory_stream.getvalue() | |
| print(f"Generated PNG size: {len(png_data)} bytes") |
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
| import aspose.svg as svg | |
| from aspose.svg.drawing import Resolution | |
| from aspose.svg.rendering.image import ImageDevice, ImageFormat, ImageRenderingOptions | |
| # Load SVG file | |
| document = svg.SVGDocument("aspose-svg-logo.svg") | |
| # Pick PNG as output | |
| options = ImageRenderingOptions() | |
| options.format = ImageFormat.PNG | |
| # Set rendering options | |
| options.page_setup.sizing.width = 600 | |
| options.page_setup.sizing.height = 400 | |
| options.horizontal_resolution = Resolution.from_dots_per_inch(96.0) | |
| options.vertical_resolution = Resolution.from_dots_per_inch(96.0) | |
| # Initialize PNG rendering device | |
| device = ImageDevice(options, "aspose-svg-logo-resized.png") | |
| # Render and save the output | |
| document.render_to(device) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment