Convert MHTML to JPG in Python
Aspose.HTML for Python via .NET supports converting MHTML to image formats such as PNG, JPG, JPEG, BMP, TIFF, and GIF. In this article, you find information on how to convert MHTML to JPEG using Aspose.HTML Python library and how to apply ImageSaveOptions.
Online MHTML Converter
You can test the API functionality and convert MHTML in real-time. Load an MHTML file from your local file system, select the output format and run the example. The conversion will be performed with default save options. You will immediately receive the result as a separate file.
If you want to convert MHTML to image formats programmatically, please see the following Python code example.
Convert MHTML to JPG using ImageSaveOptions
The convert_mhtml()
methods of the
Converter class are primarily used as the easiest way to convert an MHTML file into various formats. To convert MHTML to JPG with ImageSaveOptions
specifying, you should follow a few steps:
- Open an existing MHTML file.
- Create a new
ImageSaveOptions object with JPEG ImageFormat. By default, the
format
property is PNG. The ImageSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting MHTML to JPG. - Use the
convert_mhtml() method of the Converter class to save MHTML as a JPG image. This method takes the
stream
,options
, output file pathsave_path
and performs the conversion operation.
The following Python code snippet shows how to convert MHTML to JPG using custom save options:
1import os
2from aspose.html import *
3from aspose.html.converters import *
4from aspose.html.saving import *
5from aspose.html.drawing import *
6from aspose.pydrawing 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)
13document_path = os.path.join(input_dir, "document.mht")
14save_path = os.path.join(output_dir, "mhtml-to-jpeg.jpeg")
15
16# Open an existing MHTML file for reading
17with open(document_path, "rb") as stream:
18
19 # Create an instance of ImageSaveOptions
20 options = ImageSaveOptions()
21 options.format.JPEG
22 options.horizontal_resolution = Resolution.from_dots_per_inch(200.0)
23 options.vertical_resolution = Resolution.from_dots_per_inch(200.0)
24 options.css.media_type.SCREEN
25 options.text.text_rendering_hint = text.TextRenderingHint.CLEAR_TYPE_GRID_FIT
26
27 # Convert MHTML to JPG
28 Converter.convert_mhtml(stream, options, save_path)
In the example, we use:
format
property to specify the output image format;horizontal_resolution
andvertical_resolution
properties to set resolution for output image in pixels per inch;css.media_type
property that specify how CSS media queries are handled during the conversion process;text_rendering_hint
property that controls the quality and method of text rendering during conversions. For instance, setting it toTextRenderingHint.CLEAR_TYPE_GRID_FIT
enhances the readability of text by using ClearType technology, which improves the smoothness and sharpness of text on screens.
Save Options – ImageSaveOptions Class
The
ImageSaveOptions class in Aspose.HTML for Python via .NET offers extensive customization options for converting MHTML content to various image formats, ensuring high-quality output tailored to your needs. Here’s a more detailed description of each property of ImageSaveOptions
:
- The format property lets you specify the output image format, such as PNG, JPEG, BMP, GIF, or TIFF, with PNG as the default.
- The page_setup property enables precise control over the page layout, including size and margins, for optimal content placement.
- The background_color property sets the background color, with transparency as the default.
- The
css property, managed through
CssOptions
, dictates how CSS properties are handled during conversion. - The horizontal_resolution and vertical_resolution properties, defaulting to 300 dpi, define the pixel resolution per inch for internal and output images, enhancing image sharpness.
- The smoothing_mode property adjusts graphics rendering quality, which is crucial for achieving smooth, professional-grade images.
- The compression property specifies the compression method for TIFF files, supporting options like LZW, CCITT3, CCITT4, RLE, and NONE, which helps balance file size and image quality.
- The text property offers configurations for text rendering, ensuring clear and accurate text output in the converted images.
How to Convert MHTML to Images
Aspose.HTML for Python via .NET supports converting MHTML 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 MHTML to BMP, you need:
- to set the format property:
options.format.BMP
; - to set the extension
.bmp
in the output image file name:save_path = os.path.join(output_dir, "mhtml-to-image.bmp")
.
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.
Aspose.HTML offers a free online MHTML to JPG Converter that converts MHTML to JPG image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!