Convert MHTML to PNG in Python

To convert MHTML to PNG in Python, open the MHTML file as a binary stream, create ImageSaveOptions without a format argument, and call Converter.convert_mhtml(). ImageSaveOptions() selects PNG by default.

MHTML stores HTML and related web resources in one archive. Converting MHTML to PNG creates lossless raster images of the rendered archive and is useful for previews, screenshots, visual comparisons, and workflows that require a static image instead of an editable web archive.

Convert MHTML to PNG in Python

The basic conversion workflow is:

  1. Open the source MHTML file as a binary stream.
  2. Create ImageSaveOptions() without a format argument to select PNG output.
  3. Pass the stream, options, and output path to Converter.convert_mhtml().
 1import os
 2import aspose.html.converters as conv
 3import aspose.html.saving as sav
 4
 5input_dir = "data"
 6output_dir = "output"
 7os.makedirs(output_dir, exist_ok=True)
 8
 9input_path = os.path.join(input_dir, "document.mht")
10output_path = os.path.join(output_dir, "mhtml-page.png")
11
12# The parameterless constructor selects PNG by default
13options = sav.ImageSaveOptions()
14
15with open(input_path, "rb") as stream:
16    conv.Converter.convert_mhtml(stream, options, output_path)

An MHTML archive can render to several PNG files when its content spans multiple output pages. Each file represents one rendered page; image conversion does not combine the complete archive into one tall image.

Convert MHTML to PNG with Custom Page Settings

Use ImageSaveOptions when the PNG output requires a specific page size, margins, background color, resolution, or rendering settings. The following example configures the output page before converting MHTML to PNG:

  1. Create ImageSaveOptions() for PNG output.
  2. Configure the required page size and margins through page_setup.
  3. Open the MHTML source as a binary stream.
  4. Call Converter.convert_mhtml() with the configured options and output path.
 1import os
 2import aspose.html.converters as conv
 3import aspose.html.drawing as dr
 4import aspose.html.saving as sav
 5
 6input_dir = "data"
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9
10input_path = os.path.join(input_dir, "document.mht")
11output_path = os.path.join(output_dir, "mhtml-options.png")
12
13options = sav.ImageSaveOptions()
14options.page_setup.any_page = dr.Page(
15    dr.Size(800, 600),
16    dr.Margin(40, 40, 10, 10)
17)
18
19with open(input_path, "rb") as stream:
20    conv.Converter.convert_mhtml(stream, options, output_path)

ImageSaveOptions for MHTML to PNG

Configure only the options required by the output. The most relevant ImageSaveOptions properties for PNG conversion are:

PropertyWhat it controls
formatOutput image format. The parameterless constructor selects PNG by default.
page_setupOutput page size, margins, and page layout.
horizontal_resolution and vertical_resolutionResolution of output pages defined in physical units and internal raster images used during processing.
background_colorColor behind the rendered content. The default background is transparent.
use_antialiasingSmoothing of rendered graphics.
textText rendering configuration.

For additional page and rendering controls, see Fine-Tuning Converters.

PNG Lossless Compression and Transparency

PNG uses lossless compression, so saving the rendered page does not introduce JPEG-style compression artifacts. It is well suited to text, diagrams, interface elements, and other content that benefits from sharp edges. The result is still a raster image and does not retain editable HTML, CSS, or MHTML archive structure.

PNG supports transparency. Transparent areas can remain transparent when neither the archived page nor its CSS defines an opaque background. Set background_color only when the output requires a specific solid background.

Common MHTML to PNG Issues

IssueExplanation and fix
Conversion creates several PNG filesThe rendered content spans multiple output pages. Adjust the page setup or source layout when a single page image is required.
Images or styles are missingCheck that the required resources are present in the MHTML archive and that its MIME parts and resource references are valid.
Content is clipped or surrounded by excessive spaceConfigure a page size and margins suitable for the archived page through page_setup.
The PNG background is opaqueCheck background declarations in the archived HTML and CSS. They are rendered even though the default option background is transparent.

Related MHTML Conversion Guides

Other Platforms

FAQ

Why can one MHTML file produce several PNG images?

MHTML content is paginated during rendering, while PNG is a single-page image format. The converter can therefore create one PNG file for each rendered output page.

Does MHTML to PNG conversion preserve transparency?

PNG supports transparency, and the default ImageSaveOptions.background_color is transparent. Transparent output is possible when the archived HTML and CSS do not define an opaque page background.

Try Online MHTML to PNG Conversion

                
            

Use the free online MHTML to PNG Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when MHTML-to-PNG conversion must run programmatically in an application, service, or batch workflow.

Download complete examples and data files from GitHub.