Skip to main content

A tool that can convert HTML to an image

Project description

English description follows Japanese.

変換イメージ(Conversion Example)


概要

html2img は、HTMLを画像に変換するための軽量ツールです。指定された HTML スニペットを描画し、スクリーンショットとして保存します。内部では Playwright を使用しており、ブラウザでの忠実な描画結果を取得できます。

特徴

  • HTMLスニペット → PNG画像へ簡単変換
  • Playwright を利用して高精度レンダリング
  • ビューポートサイズのカスタマイズが可能
  • 依存最小、シンプルなAPI設計

インストール方法

pip install html2img
playwright install

⚠️ 注意 2行目の playwright install を忘れると動作しません。

使い方

import html2img

html = '<span style="color:gray;">Hello,</span><br>World!'
html2img(html, "result.png")

# ウインドウサイズ(ビューポート)の指定も可能
html2img(html, "small.png", viewport = (100, 100))

引数

  • html:描画対象の HTML 文字列
  • filename:保存先の画像ファイル名(PNG推奨)
  • viewport(任意):タプル (幅, 高さ) を指定することでレンダリング領域を調整可能

発展的な引数

  • viewport の y を None に指定すると、コンテンツの長さに応じてスクショ縦幅が自動的に調整される
  • scale を指定する (例えば4.0) と、サブピクセルレンダリングのような形で高画質で描画される

注意点

  • CSSやJavaScriptを含めることも可能ですが、外部リソースへの依存がある場合は動作しないことがあります。

Overview

html2img is a lightweight utility to convert HTML snippets into image files. It renders the given HTML using a real browser engine and captures a screenshot. Internally, it uses Playwright for precise rendering.

Features

  • Simple HTML-to-image conversion
  • High-fidelity rendering via Playwright
  • Optional viewport size configuration
  • Minimal API, easy to integrate

Installation

pip install html2img
playwright install

⚠️ Note You must run playwright install after installing the library to download the necessary browser engines.

Usage

import html2img

html = '<span style="color:gray;">Hello,</span><br>World!'
html2img(html, "result.png")

# You can also specify the window (viewport) size
html2img(html, "small.png", viewport = (100, 100))

Parameters

  • html: The HTML string to render
  • filename: Output file path (PNG recommended)
  • viewport (optional): Tuple (width, height) for controlling the viewport size

こちらが英訳です:

Advanced Arguments

  • If the y value of viewport is set to None, the screenshot height will automatically adjust according to the content length.
  • Specifying scale (e.g., 4.0) enables high-resolution rendering, similar to subpixel rendering.

Notes

  • Supports inline CSS and JavaScript. External resources may not load depending on the environment.

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

html2img-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

html2img-0.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: html2img-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for html2img-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8214d46a0bcb74d1bd4ca42e12c88afa9c79f35a662884f63a27cd41dc6efad0
MD5 266de06e024080b955520f1e15240c2e
BLAKE2b-256 abb1dcfd3a042c8832d9cac335a0985c98a21de6e9774eee6e9153fcf6d31667

See more details on using hashes here.

File details

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

File metadata

  • Download URL: html2img-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for html2img-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 141abc62467bf9bd766755afaca48d0dd1ce33a4d377eda7c5708caa760cc837
MD5 ec329a94d8a23e070c989d4802e1a46e
BLAKE2b-256 c82a26b8e70a978f9a57a582794db630ba4763c228a660f7a78b9c2ff3e04417

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