Python: HTML/CSS to Image
Generate a png, jpg or webp images with Python. Renders exactly like Google Chrome.
Table of contents
Generating images with Python
- The API takes your HTML/CSS and runs it inside a real instance of Google Chrome to convert your html into an image.
- Use Python to send the API your HTML/CSS.
- You’ll get back json with the URL to your generated image.
For more details on how this works, see Creating an image.
Example API response:
{
"url": "https://hcti.io/v1/image/be4c5118-fe19-462b-a49e-48cf72697a9d"
}
Authentication with Python
The API uses HTTP Basic authentication.
Your username is your User ID and your password is your API Key. Both of these are available from the dashboard. The Python code sample demonstrates how to authenticate your request.
You can signup for a free API key to get started.
Python example code
This Python code example sends an HTTP POST to the https://hcti.io/v1/image
API. Converting your HTML/CSS to an image with Python.
# pip3 install requests
import requests
HCTI_API_ENDPOINT = "https://hcti.io/v1/image"
# Retrieve these from https://htmlcsstoimage.com/dashboard
HCTI_API_USER_ID = 'your-user-id'
HCTI_API_KEY = 'your-api-key'
data = { 'html': "<div class='box'>Hello, world!</div>",
'css': ".box { color: white; background-color: #0f79b9; padding: 10px; font-family: Roboto }",
'google_fonts': "Roboto" }
image = requests.post(url = HCTI_API_ENDPOINT, data = data, auth=(HCTI_API_USER_ID, HCTI_API_KEY))
print("Your image URL is: %s"%image.json()['url'])
# https://hcti.io/v1/image/7ed741b8-f012-431e-8282-7eedb9910b32
Need help?
Talk to a human. Please email us support@htmlcsstoimage.com with any questions and we’ll gladly help you get started.