Skip to main content

HTML 2 Image

PyPI PyPI PyPI GitHub GitHub

HTML2Image (HTML to Image) is a lightweight Python package that acts as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files.

HTML2Image is currently in a work in progress stage.

Principle

Most web browsers have a Headless Mode, which is a way to run them without displaying any graphical interface. Headless mode is mainly used for automated testings but also comes in handy if you want to take screenshots of web pages that are exact replicas of what you would see on your screen if you were using the browser yourself.

However, for the sake of taking screenshots, headless mode is not very convenient to use. HTML2Image aims to hide the inconveniences of the browsers' headless modes while adding useful features such as allowing to create an image from as little as a string.

For more information about headless modes :

Installation

html2image is published on PyPI and can be obtained through pip or your favorite package manager :

pip install html2image

Usage

Import the library and instantiate it

from html2image import HtmlToImage
hti = HtmlToImage()

Possible arguments for the constructor :

  • browser : Browser that will be used, set by default to 'chrome' (the only browser supported by HTML2Image at the moment)
  • chrome_path and firefox_path : The path or the command that can be used to find the .exe of a specific browser. For now, start chrome is the default value of chrome_path.
  • output_path : Path to the folder to which taken screenshots will be outputed. Default is the current working directory of your python program.
  • size : 2-Tuple reprensenting the size of the screenshots that will be taken. Default value is (1920, 1080).
  • temp_path : Path that will be used by HTML2Image put together the different resources . Default value is the path in the %TEMP% user variable on windows (type echo %TEMP% in a command prompt to see it).

You can also modify these values afterward by accessing the attribute of the same name :

hti.size = (500, 200)

Image from an URL

The following code takes a screenshot (with a size of 800 * 400 ) of the python.org webpage and save it in the current working directory as python_org.png :

hti.size = (800, 400)
hti.screenshot_url('https://www.python.org', 'python_org.png')

# one line alternative :
hti.screenshot_url('https://www.python.org', 'python_org.png', size=(800, 400))

Result :

blue_screenshot

Image from HTML and CSS strings

The following code generates an image from two given strings, an HTML one and a CSS one.

...

# minimal html : quite unconventional but browsers can read it anyway
my_html_string = """\
<link rel="stylesheet" href="red_background.css">
<h1> An interesting title </h1>
This page will be red
"""

my_css_string = "body { background: red; }"

# image from html & css string
hti.load_str(my_html_string, as_filename='red_page.html')
hti.load_str(my_css_string, as_filename='red_background.css')

hti.screenshot('red_page.html', 'red.png')

Result (using size=(500, 200)):

blue_screenshot

Image from HTML and CSS files

/* blue_background.css */
body {
    background: blue;
}
<!-- blue_page.html -->
<!doctype html>
<html>
<head>
    <link rel="stylesheet" href="blue_background.css">
</head>

<body>
    <h1> An interesting title </h1>
    This page will be blue
</body>
</html>
...

# image from html & css files
hti.load_file('blue_page.html')
hti.load_file('blue_background.css')

hti.screenshot('blue_page.html', 'blue.png')

Result (using size=(500, 200)):

blue_screenshot

TODO List

  • A nice CLI
  • Suport of other browsers, such as Firefox
  • More extensive doc + comments
  • Deep search for the browsers executables?
  • PDF generation?

Metadata

Release files for html2image 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for html2image 0.1.3
File Size Uploaded Provenance
html2image-0.1.3.tar.gz 7.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for html2image 0.1.3
File Interpreter ABI Platform
html2image-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / html2image-0.1.3.tar.gz

Download URL html2image-0.1.3.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b7594b0a078126a6441b263fc77f2d75705362ede30cd74b933742ad63b43aa9
BLAKE2b-256 checksum
How to use checksums
35dea00dd7f959d6386185beae543f96c5b17a684e7db5d314c7c8297c152f12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.2 Windows/10

Release files / html2image-0.1.3-py3-none-any.whl

Download URL html2image-0.1.3-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f604104315182c44c86039b63ed8713dad9b7bbee06776d63d63563917c84411
BLAKE2b-256 checksum
How to use checksums
315e6478ab0e6db307595f92a31d9e9505ac8c81b0f090d804a7e61e46d844c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.2 Windows/10

Security for html2image 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page