Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

HTML 2 Image

HTML2Image ("HTML to Image") is 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 a work in progress and has only be used/tested on Windows for now.

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 in our case, because it can be used 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 informations 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
htmi = 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 :

htmi.size = (500, 200)

Nota Bene : If typing start chrome in a command prompt (or only chrome if you want to do this via Win+R) does not launch chrome, you will have to change this value by your chrome path, for instance by doing :

htmi.chrome_path = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'

Image from an URL

The following code takes a screenshot of the python.org webpage and save it in the current working directory as python_org.png :

htmi.url_to_img(url='https://www.python.org/', output_file='python_org.png')

Result (using size=(800, 550)):

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
htmi.load_str(my_html_string, as_filename='red_page.html')
htmi.load_str(my_css_string, as_filename='red_background.css')

htmi.render('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
htmi.load_file('blue_page.html')
htmi.load_file('blue_background.css')

htmi.render('blue_page.html', 'blue.png')

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

blue_screenshot

TODO List

  • Clean the main directory by moving the examples in their own directory
  • Support for OSX and UNIX systems
  • CLI
  • Suport of other browsers, such as Firefox
  • More extensive doc + comments
  • Check if the lib can find the browsers .EXEs

Release files for html2image 0.1.0a0

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.0a0
File Size Uploaded
html2image-0.1.0a0.tar.gz 5.6 kB Details

Built distribution (wheel)

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

Total release size: 11.3 kB

Release files / html2image-0.1.0a0.tar.gz

Download URL html2image-0.1.0a0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6462e4ac5270ba78fbba1960cf218944ce8eb3753c24bda7843b6371a62641f4
BLAKE2b-256 checksum
How to use checksums
ceb5e978ceb79f2a28df6ee6876aef833d458ae2e1f5492a421b02599674947b
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.0a0-py3-none-any.whl

Download URL html2image-0.1.0a0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60dd9ddc122e4430d1683ba5cc021088ae1ecb8d03424133bf27b16f4902f0ca
BLAKE2b-256 checksum
How to use checksums
d1e81bc9b2ca1c1c1f3a1f821a027809f67f89ddf3ee60998d6ed28b3b43c2af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.2 Windows/10

Release history Release notifications | RSS feed

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.6

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

This release

0.1.0a0 This release

2 release files

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