Convert HTML to PNG, JPEG, BMP, GIF, or TIFF Image in Python

HTML (HyperText Markup Language) is a leading file format for web pages supported by all browsers. It is frequently used to display data and information as a web page. In certain cases, we may need to convert an HTML document to an image format such as JPG, PNG, TIFF, BMP, GIF, etc. In this article, we will learn how to convert HTML to PNG, JPEG, BMP, GIF, or TIFF images in Python.

  1. Python API to Convert HTML to Image
  2. Convert HTML to JPG Image
  3. Convert HTML to PNG Image
  4. HTML to BMP Conversion
  5. Convert HTML to GIF Image
  6. Convert HTML to TIFF Image
  7. HTML String to Image Conversion

Python API to Convert HTML to Image

For converting HTML to image formats, we will be using the Aspose.Words for Python API. It is a complete solution to read and manipulate documents of various types programmatically in Python applications. It enables us to generate, modify, convert, render and print Microsoft Word (DOC, DOCX, ODT), PDF, and Web (HTML, Markdown) documents.

Please install the API from PyPI using the following pip command in the console:

> pip install aspose-words

Convert HTML to JPG Image in Python

We can easily convert HTML documents to JPG images by following the steps given below:

  1. Firstly, load the HTML file using the Document class.
  2. Optionally, specify the image save options using the ImageSaveOptions class object.
  3. Next, loop through all the pages in the document.
  4. After that, extract each page using the extract_pages() method.
  5. Finally, save the page as JPG using the save() method.

The following code sample shows how to convert HTML to JPG images in Python.

Convert HTML to JPG/JPEG image in Python

Convert HTML to PNG Image in Python

We can convert HTML documents to PNG images by following the steps given below:

  1. Firstly, load the HTML file using the Document class.
  2. Next, create an instance of the ImageSaveOptions class.
  3. Then, specify the image save options such as image_brightness, image_contrast.
  4. Next, loop through all the pages in the document.
  5. After that, extract each page using the extract_pages() method.
  6. Finally, save the page as JPG using the save() method.

The following code sample shows how to convert HTML to the PNG images in Python.

HTML to BMP Conversion in Python

We can convert HTML documents to BMP images by following the steps given below:

  1. Firstly, load the HTML file using the Document class.
  2. Next, loop through all the pages in the document.
  3. After that, extract each page using the extract_pages() method.
  4. Finally, save the page as JPG using the save() method.

The following code sample shows how to convert HTML to BMP images in Python.

Convert HTML to GIF Image in Python

Similarly, we can also convert HTML documents to GIF images by following the steps mentioned earlier. However, we just need to save the images as a GIF with the “.gif” extension in step # 4.

The following code sample shows how to convert HTML to GIF images in Python.

Convert HTML to TIFF Image in Python

We can also convert HTML documents to TIFF images by following the steps given below:

  1. Load the HTML file using the Document class.
  2. Save the document as TIFF using the save() method.

The following code sample shows how to convert an HTML document to a TIFF image in Python.

Convert HTML to TIFF image in Python

HTML String to Image Conversion in Python

We can generate an image file from an HTML string dynamically by following the steps given below:

  1. Firstly, create an instance of the Document class.
  2. Next, create an instance of the DocumentBuilder class.
  3. After that, insert HTML string using the insert_html() method.
  4. Finally, save the document as JPG using the save() method.

The following code sample shows how to convert an HTML string to a JPG image in Python.

Convert HTML string to an image in Python

Get a Free License

You can get a free temporary license to try Aspose.Words for Python without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • convert HTML documents to images programmatically;
  • convert the content of an HTML file into PNG, JPEG, BMP, GIF, or TIFF images;
  • generate an HTML document and convert it into an image using Python.

In addition, you can learn more about the API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also