Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert MHTML to JPG or JPEG in Python, open the MHTML file as a binary stream, create
ImageSaveOptions with ImageFormat.JPEG, and call
Converter.convert_mhtml() with the stream, options, and output path.
JPG and JPEG are two names for the same raster image format. JPEG uses lossy compression, which usually produces smaller files than PNG but can soften text and sharp edges. Converting MHTML to JPEG is useful for previews, thumbnails, presentations, and systems that need static images instead of a web archive.
The basic conversion workflow is:
ImageSaveOptions with ImageFormat.JPEG. PNG is the default image format, so JPEG must be selected explicitly.Converter.convert_mhtml(). 1# Convert MHTML to JPG in Python
2
3import aspose.html.converters as conv
4import aspose.html.saving as sav
5import aspose.html.rendering.image as rim
6
7# Open the MHTML file
8with open("document.mht", "rb") as stream:
9
10 # Configure JPG output
11 options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
12
13 # Convert MHTML to JPG
14 conv.Converter.convert_mhtml(stream, options, "document.jpeg")The example converts the archived page to JPEG with the default page and rendering settings. Use either .jpg or .jpeg as the output extension; both extensions represent the same image format.
Use ImageSaveOptions when the JPEG output requires a particular page size, resolution, background, antialiasing, or text-rendering setting. The following example sets the resolution and page dimensions, enables rendering-quality options, and uses an explicit background color:
ImageSaveOptions with ImageFormat.JPEG.Converter.convert_mhtml(). 1import os
2import aspose.html.converters as conv
3import aspose.html.drawing as dr
4import aspose.html.rendering.image as rim
5import aspose.html.saving as sav
6import aspose.pydrawing as pd
7
8input_dir = "data"
9output_dir = "output"
10os.makedirs(output_dir, exist_ok=True)
11
12input_path = os.path.join(input_dir, "document.mht")
13output_path = os.path.join(output_dir, "mhtml-page.jpg")
14
15options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
16options.horizontal_resolution = dr.Resolution.from_dots_per_inch(150.0)
17options.vertical_resolution = dr.Resolution.from_dots_per_inch(150.0)
18options.background_color = pd.Color.bisque
19options.page_setup.any_page = dr.Page(
20 dr.Size(600, 800),
21 dr.Margin(20, 20, 20, 20)
22)
23options.use_antialiasing = True
24options.text.use_hinting = True
25with open(input_path, "rb") as stream:
26 conv.Converter.convert_mhtml(stream, options, output_path)An MHTML archive can produce several JPG files when its content spans multiple rendered pages. Each JPEG contains one output page; unlike PDF, JPEG cannot store several pages in one file.
The most relevant ImageSaveOptions properties for this conversion are:
| Property | Use it to control |
|---|---|
| format | Output image format. Select ImageFormat.JPEG because PNG is the default. |
| page_setup | Output page dimensions, margins, and layout. |
| background_color | Color behind the rendered page and transparent source areas that JPEG cannot preserve. |
| horizontal_resolution and vertical_resolution | Raster output resolution. Higher values create more pixels and larger files. |
| css | CSS processing, including the media type used for media queries. |
| use_antialiasing | Smoothing of rasterized edges. It is enabled by default. |
| text | Text rendering settings, including font hinting. |
JPEG uses lossy compression and does not support an alpha channel. Use JPG when smaller photographic previews are more important than lossless text edges or transparency. For interface captures, diagrams, code-heavy pages, or transparent output, use MHTML to PNG instead.
Set background_color explicitly when the archived page contains transparent areas. This provides a predictable opaque background before the page is saved as JPEG. Increasing the output resolution creates more pixels, but it cannot restore detail that is absent from low-resolution source images stored in the MHTML archive.
| Issue | Likely cause and solution |
|---|---|
| Conversion creates several JPG files | The MHTML content spans multiple output pages. Adjust the page setup or source layout when one page image is required. |
| Images or styles are missing | The resource is absent from the archive, its MIME reference is invalid, or an external dependency cannot be accessed. Check the MHTML source and resource URLs. |
| Text and edges look soft | JPEG compression and insufficient raster resolution can reduce sharpness. Use an appropriate resolution or choose PNG for lossless page images. |
| Transparent areas have an unexpected color | JPEG cannot preserve transparency. Set background_color to the required opaque color before conversion. |
| Content is clipped or surrounded by excessive space | Configure page dimensions and margins that match the archived page through page_setup. |
Yes. JPG and JPEG use the same lossy image format. The shorter .jpg extension became common on systems that historically limited file extensions to three characters.
JPEG cannot store transparent pixels. Set ImageSaveOptions.background_color to a suitable opaque color when the archived page contains transparent areas. Use PNG if transparency must be retained.
Use the free online MHTML to JPG Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when MHTML-to-JPEG conversion must run programmatically in an application, service, or batch workflow.
Download complete examples and data files from GitHub.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.