Skip to main content

Package acting as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files.

Project description

HTML 2 Image

PyPI PyPI PyPI

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 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 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
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 of the python.org webpage and save it in the current working directory as python_org.png :

hti.screenshot_url('https://www.python.org/', '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
hti.load_str(my_html_string, 'red_page.html')
hti.load_str(my_css_string, '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

  • Carry tests on 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

html2image-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

html2image-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file html2image-0.1.0.tar.gz.

File metadata

  • Download URL: html2image-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.2 Windows/10

File hashes

Hashes for html2image-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c2bafcefe39a0bfaedf2c14f13d070150c48b1392748578a4bb9aaa71822a3dd
MD5 6eedcfe0024a58c755ea2936b975fb24
BLAKE2b-256 fae18c2cd3ad37fa4df04c7a244555ece0721012d60de96cae40668e8688b6a6

See more details on using hashes here.

File details

Details for the file html2image-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: html2image-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.2 Windows/10

File hashes

Hashes for html2image-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 676952aa56227ce5369e08e443dbdc4b711b1d6876a03385c82795930924c346
MD5 c6a44a37fd8d7b39256feaacf3531371
BLAKE2b-256 e11ad1055f5b77a06ff10115cdd4042a82dab9987313003590f3bb118eb904fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page