How to Convert SVG to PNG High Quality in Python

Convert SVG to PNG High Quality in Python

SVG or scalable vector graphics is a lightweight vector file format and XML-based markup language.
It is used for two-dimensional vector and mixed vector or raster graphics. PNG is a raster-graphics file format that supports lossless data compression. It was designed to improve the gif file format. SVG does not support as much details like standard image formats. But PNG is capable of handling very high resolutions and can preserve transparency. For such scenarios, you can convert a SVG file to PNG format. So let’s have a look at how to convert SVG to PNG high quality in Python.

The following topics shall be covered in this article:

API for Converting SVG Images to PNG Files and Python SDK

To convert SVG to PNG image using Python, we will be using Python SDK of GroupDocs.Conversion Cloud API. Our Python library provides the best and secure way to convert SVG to PNG file quickly. It is 100% free, secure and easy to use Python SDK for image conversion. It allows supported formats conversion to images programmatically on the cloud. Please install it using the following command in the console:

pip install groupdocs_converison_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

# Import Python SDK in your python application from http://api.groupdocs.cloud
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "InternalStorage"

How to Convert SVG into PNG Online in Python using REST API

You may convert SVG code to PNG online by following the simple steps as listed below:

  1. Upload the SVG file to the cloud
  2. Convert SVG to PNG without losing quality in Python
  3. Download the converted PNG file

Upload the Image

Firstly, upload the SVG file to the Cloud using the following code sample:

# Upload SVG file to your cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-file.svg", "H:\\groupdocs-cloud-data\\sample-file.svg", storage_name)
# Upload docx file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)

As a result, the uploaded SVG file will be available in the files section of your dashboard on the cloud.

Python Convert SVG to PNG High Quality

You can easily convert SVG to PNG transparent background online programmatically by following the steps mentioned below:

  • Create an instance of ConvertApi
  • Create ConvertSettings instance
  • Set the SVG file path
  • Assign “png” to format
  • Provide output file path
  • Create ConvertDocumentRequest
  • Get results by calling the ConvertApi.convertDocument() method

The following code example shows how to convert online SVG to PNG without losing quality using REST API in Python:

# How to Convert SVG into PNG Online in Python using REST API
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-file.svg"
settings.format = "png"
settings.output_path = "python-testing"
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
response = convert_api.convert_document(request)
print("Successfully converted SVG to PNG format: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception while calling API: {0}".format(e.message))
How to Convert SVG to PNG with transparent background Online in Python

Convert SVG to PNG Online High Quality in Python

Download the Converted File

The above code sample will save the converted PNG file on the cloud. You can download it using the following code sample:

# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-file.png", storage_name)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded image png file to your local directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")

Online Convert SVG to PNG in Python using Advanced Options

In python convert SVG to PNG while resizing and increasing quality. Please follow the steps to convert SVG image to PNG with some advanced settings as show below:

  • Create an instance of ConvertApi
  • Create ConvertSettings instance
  • Set the SVG file path
  • Assign “png” to format
  • Provide output file path
  • Define PngConvertOptions
  • Set various convert settings such as dpi, imageQuality, height, margins (top, left, right, bottom), etc.
  • Create ConvertDocumentRequest
  • Get results by calling the ConvertApi.convertDocument() method

The following code example shows how to turn SVG into PNG high resolution using advanced convert options:

# How to Online Convert SVG to PNG High Quality in Python using Advanced Options
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-file.svg"
settings.format = "png"
convertOptions = groupdocs_conversion_cloud.PngConvertOptions()
convertOptions.gray_scale = True
convertOptions.from_page = 1
convertOptions.pages_count = 1
convertOptions.quality = 100
convertOptions.rotate_angle = 90
convertOptions.use_pdf = False
settings.convert_options = convertOptions
settings.output_path = "python-testing"
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
response = convert_api.convert_document(request)
print("Successfully converted SVG to PNG format: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception while calling API: {0}".format(e.message))

Free SVG to PNG Converter Online

How to convert SVG to PNG online free? Please try the following online SVG to PNG converter for free. It is the best SVG to PNG converter to convert SVG to PNG with transparent background and is developed using the above API.

Conclusion

In this article, you have learned:

  • how to convert SVG into PNG online in Python on the cloud;
  • how to turn SVG to PNG in Python using advanced options;
  • programmatically upload the file and then download the converted file from the cloud;
  • SVG to PNG converter online free;

You can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser.

Ask a question

You can ask your queries about SVG to PNG file converter API, via our Free Support Forum

FAQs

How to render SVG image to PNG file in Python?

Please follow this link to learn the Python code snippet about how to convert SVG to PNG without losing quality.

How to convert an SVG to PNG online using REST API?

Create an instance of ConvertApi, set the values of the convert settings and invoke the convertDocument method with ConvertDocumentRequest to save SVG as PNG high resolution.

How to change an SVG to PNG free online?

There is a free online SVG to PNG file converter that allows you to convert SVG to transparent PNG image, quickly and easily. Once the conversion is completed, you can download the PNG file stored on the cloud.

How to Convert SVG to PNG with Python on Windows?

Please visit the Link to easily convert SVG to PNG high resolution unlimited files on your own Windows PC.

How to install SVG to PNG converter free download library?

You can install SVG to PNG Python library to create, and process SVG to PNG conversion programmatically.

See Also