Convert HTML to GIF in Python
GIF files are widely supported across different devices and applications, making them a versatile choice for sharing snippets of web content or visual tutorials. Their small file size ensures quick loading and easy distribution. Using Converter.convert_html() methods is the most common way to convert HTML to GIF.
This article provides information on converting HTML to GIF using the convert_html()
methods of the
Converter class and applying
ImageSaveOptions.
To continue following this tutorial, install and configure the Aspose.HTML for Python via .NET in your Python project. Our code examples help you to convert HTML to GIF and generate GIF files using the Python library.
Online HTML Converter
You can check the Aspose.HTML for Python via .NET API functionality and convert HTML in real-time. Load HTML from your local file system or a URL, select the output format and run the example. The example uses the default save options, allowing for a simple conversion process. You will immediately receive the result as a separate file.
Convert HTML to GIF – Python Code Example
The following example shows how to use ImageSaveOptions
and create the output GIF image with custom save options such as an image format, resolution, and css media_type:
- Load an HTML file using the HTMLDocument class.
- Create a new
ImageSaveOptions object with GIF ImageFormat. By default, the
format
property is PNG. TheImageSaveOptions()
constructor initializes an instance of theImageSaveOptions
class that is passed toconvert_html()
method. Here, you can set the required save options, such as format or resolution. - Use the
convert_html() method of the Converter class to save HTML as a GIF image. The method takes the
document
,options
, output file pathsave_path
and performs the conversion operation.
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, "document.html")
15save_path = os.path.join(output_dir, "html-to-image.gif")
16
17# Initialize an HTML document from the file
18document = HTMLDocument(document_path)
19
20# Initialize ImageSaveOptions
21options = ImageSaveOptions(ImageFormat.GIF)
22options.horizontal_resolution = Resolution.from_dots_per_inch(96.0)
23options.vertical_resolution = Resolution.from_dots_per_inch(96.0)
24options.css.media_type.PRINT
25
26# Convert HTML to GIF
27Converter.convert_html(document, options, save_path)
Save Options – ImageSaveOptions Class
The GIF images creation functionality can be enhanced with save options per your needs. Aspose.HTML for Python via .NET allows converting HTML to GIF using default or custom save options. The
ImageSaveOptions class offers extensive customization for converting HTML content into image formats. Here’s 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 supports converting HTML to PNG, JPG, JPEG, BMP, TIFF, and GIF images. You can use the above Python code for this; to set the output image format, you only need to specify the required extension (format) in the output file name and set the format
property for the save options object.
For example, to convert HTML to BMP, you need:
- to set the format property:
options = ImageSaveOptions(ImageFormat.BMP)
; - to set the extension
.bmp
in the output image file name:save_path = os.path.join(output_dir, "html-to-image.bmp")
.
Download the Aspose.HTMLfor Python via .NET library to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
Aspose.HTML offers a free online HTML to GIF Converter that converts HTML to GIF image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!