Skip to main content

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.

Metadata

Release files for html2img 0.1.0

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

Source distribution (sdist)

Source distribution for html2img 0.1.0
File Size Uploaded Provenance
html2img-0.1.0.tar.gz 3.9 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 8.3 kB

Release files / html2img-0.1.0.tar.gz

Download URL html2img-0.1.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8214d46a0bcb74d1bd4ca42e12c88afa9c79f35a662884f63a27cd41dc6efad0
BLAKE2b-256 checksum
How to use checksums
abb1dcfd3a042c8832d9cac335a0985c98a21de6e9774eee6e9153fcf6d31667
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.3

Release files / html2img-0.1.0-py3-none-any.whl

Download URL html2img-0.1.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
141abc62467bf9bd766755afaca48d0dd1ce33a4d377eda7c5708caa760cc837
BLAKE2b-256 checksum
How to use checksums
c82a26b8e70a978f9a57a582794db630ba4763c228a660f7a78b9c2ff3e04417
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.3

Security for html2img 0.1.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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