Convert HTML to PNG – Aspose.HTML for Python via .NET
PNG is one of the most used image file formats. It is popular due to its lossless compression, which ensures that images retain their original quality without any data loss, making it ideal for images that require high fidelity, such as web graphics, logos, and detailed illustrations. Additionally, PNG supports transparency, allowing for flexible design options and integration with various backgrounds. Converting HTML to PNG allows you to save web content as high-quality images, which can be easily shared and embedded in presentations or reports without the risk of altering the original layout and design. With Aspose.HTML for Python via .NET, you can convert HTML to PNG programmatically with full control over a wide range of conversion parameters.
In this article, you find information on how to convert HTML to PNG by using convert_html() methods of the Converter class, and how to apply ImageSaveOptions.
To continue following this tutorial, install and configure Aspose.HTML for Python via .NET in your Python project. Our code examples help you to convert HTML to PDF and generate PDF files using the Python library.
Online HTML Converter
You can test the functionality of the Aspose.HTML for Python via .NET API and perform real-time HTML conversions. Load an HTML file from your local system or a URL, select the desired output format, and run the example. Default save options are applied, and you will receive the converted file instantly.
Convert HTML to PNG – Python Code Example
The Python code snippet above shows how to convert HTML to PNG with default save options. You should follow a few steps:
- Load an HTML file using the HTMLDocument class.
- Create a new
ImageSaveOptions object. By default, the
format
property is PNG. TheImageSaveOptions()
constructor initializes an instance of theImageSaveOptions
class that is passed toconvert_html()
method. - Use the
convert_html() method of the Converter class to save HTML as a PNG image. The method takes the
document
,options
, output file pathsave_path
and performs the conversion operation.
HTML to PNG by a single line of code
The methods of the Converter class are primarily used as the easiest way to convert an HTML code into various formats. You can convert HTML to PNG with default save options in your Python application literally with a single line of code!
1from aspose.html import *
2from aspose.html.converters import *
3from aspose.html.saving import *
4
5# Convert HTML to PNG
6Converter.convert_html("document.html", ImageSaveOptions(), "document.png")
Convert HTML to PNG using ImageSaveOptions
You can convert HTML to PNG using default or custom save options. The following Python code example shows how to use ImageSaveOptions to customize the result of the conversion:
1import os
2from aspose.html import *
3from aspose.html.converters import *
4from aspose.html.saving import *
5from aspose.html.drawing import *
6from aspose.html.rendering.image import *
7
8# Setup directories and define paths
9output_dir = "output/"
10input_dir = "data/"
11if not os.path.exists(output_dir):
12 os.makedirs(output_dir)
13
14document_path = os.path.join(input_dir, "banner.html")
15save_path = os.path.join(output_dir, "banner-options-resolution.png")
16
17# Initialize an HTML document from the file
18document = HTMLDocument(document_path)
19
20# Initialize ImageSaveOptions
21options = ImageSaveOptions()
22options.horizontal_resolution = Resolution.from_dots_per_inch(200.0)
23options.vertical_resolution = Resolution.from_dots_per_inch(200.0)
24
25# Convert HTML to PNG
26Converter.convert_html(document, options, save_path)
You can evaluate the quality of conversion by trying our product. The following figure shows a fragment of an A4-size image – the result of converting a banner.html file to a PNG image. You can compare the source with the result and see that the banner size has changed when converted compared to its size on the web page. This is because since we did not specify the image size for conversion, it was set to default – the resulting image width and height correspond to A4, and all margins have zero value:
Save Options – ImageSaveOptions Class
The
ImageSaveOptions class offers extensive customization for converting HTML content into image formats, ensuring that the resulting images meet specific quality, format, and rendering requirements based on the intended application or use case. Here is a more detailed description of each property of ImageSaveOptions
:
- page_setup – You can configure the page layout settings for the output image. This includes specifying the page size and margins (top, bottom, left, right) to control the placement and display of HTML content within the image.
- horizontal_resolution – This property sets or gets the horizontal resolution (in pixels per inch) for both output and internal images used during processing. Higher resolution usually results in a sharper image but can also increase file size. By default, it is 300 dpi.
- vertical_resolution – This property sets or gets the vertical resolution for internal images in pixels per inch. By default, it is 300 dpi.
- background_color – This property allows you to set the background color for the rendered output. If not set, the default background is transparent.
- css – This property, represented by
CssOptions
, allows configuring how CSS properties are processed during the HTML to image conversion. - format – This property determines the format of the output image. The supported formats include common image formats like PNG, JPEG, BMP, GIF, and TIFF. The default format is PNG, but you can specify others based on your requirements.
- smoothing_mode – This property controls the quality of graphics rendering during conversion. It affects how images are rendered, which is especially useful for anti-aliasing and achieving smooth and visually appealing output. Options typically include settings for high-quality rendering, which can be critical for professional and presentation-grade images.
- compression – The compression option allows you to set the compression method for TIFF output. Supported options: LZW, CCITT3, CCITT4, RLE, and NONE. Compression helps reduce file size while maintaining image quality, which is especially important for TIFF files used for high-quality archiving and printing of images.
- text – This property provides configurations for text rendering during HTML to image conversion.
How to Convert HTML to Images
Aspose.HTML for Python via .NET allows you to convert HTML to various image formats such as PNG, JPG, JPEG, BMP, TIFF, and GIF. You can use the provided Python code for these conversions. To specify the desired image format, set the format
property in the save options object and include the corresponding file extension in the output file name.
For example, to convert HTML to BMP:
- Set the format property:
options = ImageSaveOptions(ImageFormat.BMP)
- Include the
.bmp
extension in the output image file name:save_path = os.path.join(output_dir, "html-to-image.bmp")
Aspose.HTML offers a free online HTML to PNG Converter that converts HTML to PNG image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!