Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
The JPEG format is popular primarily due to its efficient compression algorithm, which allows high-quality images to be stored in relatively small file sizes. This makes JPEG ideal for sharing and displaying images on the web, where loading speed is critical. Converting HTML to JPEG becomes necessary in scenarios where web content needs to be embedded into documents like presentations or emails, preserving the visual fidelity of the web page in a widely supported image format. This conversion ensures that complex web layouts, graphics, and text accurately represent a static image suitable for various applications and devices. Using Converter.convert_html() methods is the most common way to convert HTML into various formats.
This article provides information on converting HTML to JPEG using the convert_html() methods of the
Converter class and applying
ImageSaveOptions. Also, you can try an Online HTML Converter to test the Aspose.HTML functionality and convert HTML on the fly.
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 JPEG and generate JPEG files using the Python library.
You can check the Aspose.HTML for Python via .NET API functionality and convert HTML in real-time. Please 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.
The following example shows how to use ImageSaveOptions and create the output JPEG image with custom save options such as an image format, page size, resolution, and css media_type:
format property is PNG. The ImageSaveOptions() constructor initializes an instance of the ImageSaveOptions class that is passed to convert_html() method. Here, you can set the required save options, such as page setup or resolution.document, options, output file path save_path and performs the conversion operation. 1# Convert HTML to JPEG using Python with custom settings
2
3import os
4import aspose.html as ah
5import aspose.html.converters as conv
6import aspose.html.saving as sav
7import aspose.html.rendering.image as rim
8import aspose.html.drawing as dr
9import aspose.pydrawing as pd
10
11
12# Setup directories and define paths
13output_dir = "output/"
14input_dir = "data/"
15if not os.path.exists(output_dir):
16 os.makedirs(output_dir)
17document_path = os.path.join(input_dir, "document.html")
18save_path = os.path.join(output_dir, "convert-html-with-options.jpg")
19
20# Load an HTML document from a file or URL
21document = ah.HTMLDocument(document_path)
22
23# Initialize saving options
24options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
25options.horizontal_resolution = dr.Resolution.from_dots_per_inch(50.0)
26options.vertical_resolution = dr.Resolution.from_dots_per_inch(50.0)
27options.background_color = pd.Color.bisque
28options.page_setup.any_page = dr.Page(dr.Size(600, 500), dr.Margin(10, 10, 10, 10))
29
30options.use_antialiasing = True
31options.text.use_hinting = True
32options.css.media_type.SCREEN
33
34# Convert HTML to JPEG
35conv.Converter.convert_html(document, options, save_path)The JPEG images creation functionality can be enhanced with save options per your needs. Aspose.HTML for Python via .NET allows converting HTML to JPEG 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:
CssOptions, allows configuring how CSS properties are processed during the HTML to image conversion.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 TIFF:
options = sav.ImageSaveOptions(rim.ImageFormat.TIFF)..tiff extension in the output image file name: save_path = os.path.join(output_dir, "html-to-image.tiff").Download the Aspose.HTML for Python via .NET library to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
You can download the complete examples and data files from GitHub.
Aspose.HTML offers a free online HTML to JPG Converter that converts HTML to JPG image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.