Convert HTML to JPEG in Python

To convert HTML to JPEG in Python, call Converter.convert_html() with an HTML source, ImageSaveOptions initialized for JPEG, and an output path. The source can be an HTML file, an HTMLDocument, inline HTML with a base URI, or a webpage URL supported by the selected overload.

JPEG and JPG are names for the same lossy image format. JPEG is widely supported and can produce smaller files than PNG, making it useful for webpage previews, reports, and content dominated by photographs. It does not support transparency and may introduce compression artifacts around small text and sharp edges.

Convert HTML to JPEG – Python Example

The basic workflow explicitly selects JPEG because PNG is the default format of ImageSaveOptions:

  1. Load or specify the source HTML file.
  2. Create ImageSaveOptions with ImageFormat.JPEG.
  3. Call Converter.convert_html() with the source, options, and output JPEG path.
 1# Convert HTML to JPEG in Python
 2
 3import aspose.html as ah
 4import aspose.html.converters as conv
 5import aspose.html.saving as sav
 6import aspose.html.rendering.image as rim
 7
 8# Load the HTML document
 9with ah.HTMLDocument("document.html") as document:
10
11    # Configure JPEG output
12    options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
13
14    # Convert HTML to JPEG
15    conv.Converter.convert_html(document, options, "output.jpeg")

The example loads document.html, selects JPEG output, and writes output.jpeg.

Convert a Webpage URL to JPEG in Python

Use a Url object to render an online HTML page as JPEG:

  1. Create a Url object for the webpage.
  2. Create JPEG save options and specify an output path.
  3. Pass the URL, options, and output path to Converter.convert_html().
 1import os
 2import aspose.html as ah
 3import aspose.html.converters as conv
 4import aspose.html.rendering.image as rim
 5import aspose.html.saving as sav
 6
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9
10url = ah.Url("https://docs.aspose.com/html/files/aspose.html")
11output_path = os.path.join(output_dir, "aspose-from-url.jpg")
12
13options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
14conv.Converter.convert_html(url, options, output_path)

The application must have network access to the page and its linked resources. A complex website can take longer to process while external resources are loaded.

Customize HTML to JPEG with ImageSaveOptions

Use ImageSaveOptions to control the page layout, background, CSS media type, antialiasing, and text hinting:

  1. Load the HTML source into an HTMLDocument.
  2. Create ImageSaveOptions for JPEG and configure the required rendering settings.
  3. Convert the document and save the JPEG output.
 1import os
 2import aspose.html as ah
 3import aspose.html.converters as conv
 4import aspose.html.drawing as dr
 5import aspose.html.rendering.image as rim
 6import aspose.html.saving as sav
 7import aspose.pydrawing as pd
 8
 9input_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13input_path = os.path.join(input_dir, "document.html")
14output_path = os.path.join(output_dir, "document-options.jpg")
15
16with ah.HTMLDocument(input_path) as document:
17    options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
18    options.page_setup.any_page = dr.Page(
19        dr.Size(600, 500),
20        dr.Margin(10, 10, 10, 10)
21    )
22    options.background_color = pd.Color.bisque
23    options.use_antialiasing = True
24    options.text.use_hinting = True
25    options.css.media_type = options.css.media_type.SCREEN
26
27    conv.Converter.convert_html(document, options, output_path)

The example creates a 600 × 500 pixel page with 10-pixel margins, uses a solid background because JPEG has no alpha channel, and explicitly applies screen-media CSS. Antialiasing is enabled by default; the assignment makes the selected rendering behavior visible in the example.

Control JPEG Dimensions, Background, and Sharpness

JPEG is suitable for photographs and continuous-tone images, but its lossy compression can soften text and sharp edges. ImageSaveOptions does not expose a separate JPEG compression-quality property.

For shared page, resolution, CSS, and rasterization settings, see Configure Image Output with ImageSaveOptions.

Common HTML to JPEG Conversion Issues

ProblemCheck or fix
The output has an unexpected backgroundJPEG cannot store transparency. Set background_color explicitly.
Text or sharp edges look blurredIncrease the actual output pixel dimensions, keep antialiasing enabled, and test font hinting. JPEG compression may still soften sharp edges.
A JPEG quality setting cannot be foundImageSaveOptions does not provide a separate JPEG compression-quality property. Its compression property is for TIFF output.

For pagination, page sizing, missing resources, and URL-loading problems, see Common HTML to Image Conversion Issues.

Related HTML Conversion Guides

Other Platforms

FAQ

Are JPG and JPEG the same format?

Yes. .jpg and .jpeg are file extensions for the same JPEG image format. Use ImageFormat.JPEG with either extension.

Can I set JPEG compression quality with ImageSaveOptions?

No separate JPEG compression-quality property is exposed by ImageSaveOptions. Configure the rendered dimensions, antialiasing, text hinting, background, fonts, and source images to control the visible result.

Can I convert an HTML string to JPEG?

Yes. Use the Converter.convert_html() overload that accepts the HTML string, a base URI, JPEG save options, and an output path. The base URI is required when the string contains relative resource URLs.

Can I convert HTML to JPEG for free?

Use the free online HTML to JPG Converter below for occasional manual conversions. For programmatic conversion, you can evaluate the library without a license or follow the licensing guide to request a temporary license.

Try Online HTML to JPG Conversion

                
            

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

Free Online HTML to JPG Converter