Convert MHTML to JPG in Python

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.

Convert MHTML to JPG in Python

The basic conversion workflow is:

  1. Open the source MHTML file as a binary stream.
  2. Create ImageSaveOptions with ImageFormat.JPEG. PNG is the default image format, so JPEG must be selected explicitly.
  3. Pass the stream, options, and output path to 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.

Customize MHTML to JPG with ImageSaveOptions

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:

  1. Create ImageSaveOptions with ImageFormat.JPEG.
  2. Configure the required resolution, page layout, and background.
  3. Open the MHTML file as a binary stream and call 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.

ImageSaveOptions for MHTML to JPEG

The most relevant ImageSaveOptions properties for this conversion are:

PropertyUse it to control
formatOutput image format. Select ImageFormat.JPEG because PNG is the default.
page_setupOutput page dimensions, margins, and layout.
background_colorColor behind the rendered page and transparent source areas that JPEG cannot preserve.
horizontal_resolution and vertical_resolutionRaster output resolution. Higher values create more pixels and larger files.
cssCSS processing, including the media type used for media queries.
use_antialiasingSmoothing of rasterized edges. It is enabled by default.
textText rendering settings, including font hinting.

JPEG Compression and Background

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.

Common MHTML to JPG Issues

IssueLikely cause and solution
Conversion creates several JPG filesThe MHTML content spans multiple output pages. Adjust the page setup or source layout when one page image is required.
Images or styles are missingThe 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 softJPEG compression and insufficient raster resolution can reduce sharpness. Use an appropriate resolution or choose PNG for lossless page images.
Transparent areas have an unexpected colorJPEG cannot preserve transparency. Set background_color to the required opaque color before conversion.
Content is clipped or surrounded by excessive spaceConfigure page dimensions and margins that match the archived page through page_setup.

Related MHTML Conversion Guides

Other Platforms

FAQ

Are JPG and JPEG the same format?

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.

Why should I set a background color for JPG output?

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.

Try Online MHTML to JPG Conversion

                
            

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.

Free Online MHTML to JPG Converter