Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created August 2, 2023 15:02

Aspose.Imaging API allows allows programmatically convert your svg images to various image formats with high quality in your Python application or Web service.

You can:

  • convert svg to raster image;
  • convert svg to vector image.

Interested ?

You may go further at : https://products.aspose.com/imaging/python-net/

import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of ApngOptions
export_options = ApngOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to apng
image.save(os.path.join(output_folder, "svg-to-apng-output.apng"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of BmpOptions
export_options = BmpOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to bmp
image.save(os.path.join(output_folder, "svg-to-bmp-output.bmp"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of DicomOptions
export_options = DicomOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to dicom
image.save(os.path.join(output_folder, "svg-to-dicom-output.dicom"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = DxfOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to dxf
image.save(os.path.join(output_folder, "svg-to-dxf-output.dxf"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of EmfOptions
export_options = EmfOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to emf
image.save(os.path.join(output_folder, "svg-to-emf-output.emf"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = EmfOptions()
export_options.compress = True
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to emz
image.save(os.path.join(output_folder, "svg-to-emz-output.emz"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of GifOptions
export_options = GifOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to gif
image.save(os.path.join(output_folder, "svg-to-gif-output.gif"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = Html5CanvasOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to html
image.save(os.path.join(output_folder, "svg-to-html-output.html"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of IcoOptions
export_options = IcoOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to ico
image.save(os.path.join(output_folder, "svg-to-ico-output.ico"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
from aspose.imaging.fileformats.jpeg2000 import Jpeg2000Codec
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = Jpeg2000Options()
export_options.codec = Jpeg2000Codec.J2K
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to j2k
image.save(os.path.join(output_folder, "svg-to-j2k-output.j2k"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
from aspose.imaging.fileformats.jpeg2000 import Jpeg2000Codec
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = Jpeg2000Options()
export_options.codec = Jpeg2000Codec.JP2
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to jp2
image.save(os.path.join(output_folder, "svg-to-jp2-output.jp2"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of JpegOptions
export_options = JpegOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to jpeg
image.save(os.path.join(output_folder, "svg-to-jpeg-output.jpeg"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of Jpeg2000Options
export_options = Jpeg2000Options()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to jpeg2000
image.save(os.path.join(output_folder, "svg-to-jpeg2000-output.jpeg2000"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = JpegOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to jpg
image.save(os.path.join(output_folder, "svg-to-jpg-output.jpeg"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of PdfOptions
export_options = PdfOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to pdf
image.save(os.path.join(output_folder, "svg-to-pdf-output.pdf"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of PngOptions
export_options = PngOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to png
image.save(os.path.join(output_folder, "svg-to-png-output.png"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of PsdOptions
export_options = PsdOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to psd
image.save(os.path.join(output_folder, "svg-to-psd-output.psd"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of SvgOptions
export_options = SvgOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to svg
image.save(os.path.join(output_folder, "svg-to-svg-output.svg"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = SvgOptions()
export_options.compress = True
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to svgz
image.save(os.path.join(output_folder, "svg-to-svgz-output.svgz"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of TgaOptions
export_options = TgaOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to tga
image.save(os.path.join(output_folder, "svg-to-tga-output.tga"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = TiffOptions(TiffExpectedFormat.DEFAULT)
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to tif
image.save(os.path.join(output_folder, "svg-to-tif-output.tiff"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = TiffOptions(TiffExpectedFormat.DEFAULT)
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to tiff
image.save(os.path.join(output_folder, "svg-to-tiff-output.tiff"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = WebPOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to webp
image.save(os.path.join(output_folder, "svg-to-webp-output.webp"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
# Create an instance of WmfOptions
export_options = WmfOptions()
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to wmf
image.save(os.path.join(output_folder, "svg-to-wmf-output.wmf"), export_options)
import os
from aspose.imaging import Image
from aspose.imaging.imageoptions import *
# You can get all image templates from https://github.com/aspose-imaging/Aspose.Imaging-for-Python-Net/blob/master/Examples/data/Templates.zip
# After download archive please unpack it and replace templatesFolder variable path with your path to unpacked archive folder
# get path of the input data
templates_folder = os.environ["DATA_PATH"] if "DATA_PATH" in os.environ else "data"
# get output path
output_folder = os.environ["OUT_PATH"] if "OUT_PATH" in os.environ else "out"
# Load the svg file in an instance of Image
with Image.load(os.path.join(templates_folder, "template.svg")) as image:
export_options = WmfOptions()
export_options.compress = True
vector_options = SvgRasterizationOptions()
vector_options.page_width = image.width
vector_options.page_height = image.height
export_options.vector_rasterization_options = vector_options
# Save svg to wmz
image.save(os.path.join(output_folder, "svg-to-wmz-output.wmz"), export_options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment