Convert HTML to Image in Python

To convert HTML to an image in Python, pass an HTML file, HTMLDocument, or Url to Converter.convert_html() with ImageSaveOptions and an output path. Select PNG, JPEG, GIF, BMP, TIFF, or WebP with ImageFormat.

HTML-to-image conversion renders HTML and CSS as static raster output. It is useful for webpage previews, thumbnails, reports, documentation, and content snapshots. The resulting images do not preserve the interactivity of links or form controls, or other HTML behavior.

Convert HTML to Image in Python

  1. Provide the HTML source as a file path, an HTMLDocument, or a Url.
  2. Create ImageSaveOptions and select the required ImageFormat.
  3. Configure page size, resolution, background, CSS, or rendering settings when needed.
  4. Call Converter.convert_html() with the source, options, and output path.

The following example converts banner.html to WebP and explicitly selects the output format:

 1import os
 2import aspose.html.converters as conv
 3import aspose.html.rendering.image as rim
 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, "banner.html")
11output_path = os.path.join(output_dir, "banner.webp")
12
13options = sav.ImageSaveOptions(rim.ImageFormat.WEBP)
14conv.Converter.convert_html(input_path, options, output_path)

The example reads the HTML file, selects ImageFormat.WEBP, and writes banner.webp. To create another raster format, use the corresponding ImageFormat value and a matching output extension.

Choose an Image Format

Choose the output format according to the source content and required image characteristics:

FormatImageFormat valueUse it for
PNGImageFormat.PNGLossless output, text, diagrams, interface previews, and transparency. PNG is the default format.
JPEGImageFormat.JPEGPhotographs and smaller lossy files when transparency is not required.
GIFImageFormat.GIFStatic graphics with a limited color palette. HTML-to-GIF conversion does not capture animation frames.
BMPImageFormat.BMPBitmap workflows where broad compatibility is more important than file size.
TIFFImageFormat.TIFFImage workflows that require TIFF output and configurable TIFF compression.
WebPImageFormat.WEBPWeb-oriented raster output with efficient compression.

Use .png, .jpg or .jpeg, .gif, .bmp, .tif or .tiff, and .webp extensions so that the file name matches the selected format. For format-specific examples, see Convert HTML to PNG, Convert HTML to JPEG, and Convert HTML to GIF.

Configure Image Output with ImageSaveOptions

ImageSaveOptions controls the selected image format and the shared rendering settings used during HTML-to-image conversion.

PropertyUse it to control
formatOutput image format. The default is PNG.
page_setupOutput page size, margins, and pagination.
horizontal_resolution and vertical_resolutionResolution of output pages defined in physical units and internal raster images used during processing. The default is 300 dpi.
background_colorBackground color of each output page. The default is transparent.
cssCSS processing settings, including the media type used during rendering.
use_antialiasingSmoothing of rasterized edges. It is enabled by default.
textText-rendering settings, including font hinting.
compressionTIFF compression. It does not set JPEG quality.

When a page is defined in pixels, its width and height determine the output raster dimensions directly. When a page is defined in physical units such as millimeters or inches, the horizontal and vertical resolution determine the resulting pixel dimensions. Increasing DPI does not enlarge a page whose dimensions are already specified in pixels.

Common HTML to Image Conversion Issues

ProblemCheck or fix
The image has unexpected page dimensionsConfigure page_setup.any_page.size. Default page settings can produce a document-sized image rather than a tightly cropped screenshot.
Conversion creates several numbered filesThe rendered content spans multiple output pages. Adjust the page size or source layout when a single image is required.
Text or images look blurredIncrease the actual output pixel dimensions and use sufficiently detailed source images. DPI does not enlarge a page already defined in pixels.
CSS, images, or fonts are missingEnsure relative URLs resolve against the document location or base URI and that linked resources are accessible.
URL conversion takes a long timeCheck for large, slow, or unavailable linked resources. Use a stable URL that the application can access.
Colors or transparency differ from the sourceCheck the selected output format. JPEG has no transparency, while GIF has a limited palette and no partial alpha transparency.

Related HTML Conversion Guides

Other Platforms

FAQ

Can I convert a webpage URL to an image?

Yes. Create a Url, select the required image format with ImageSaveOptions, and pass the URL, options, and output path to Converter.convert_html(). The application must be able to access the webpage and its linked resources.

Does HTML-to-image conversion create a screenshot?

It creates a static rendered image of the HTML document using the configured page and rendering options. It does not capture browser controls, user interaction, or a sequence of animation frames.

Why does HTML-to-image conversion create multiple files?

Image output is page-based. When the rendered HTML spans several pages, the converter creates an image for each page. Configure the page size or adjust the source layout when one output image is required.

Can I convert HTML to an image for free?

Use the free online converter below for occasional manual conversion. For programmatic conversion, evaluate the library or follow the licensing guide to request a temporary license.

Try Online HTML to Image Conversion

Use the free online HTML to Image Converter for quick manual conversion without writing code. Use Aspose.HTML for Python via .NET when image conversion must run in a Python application, service, or document-processing workflow.