728x90

HTML 코드를 이미지로 변환하는 방법 (Html2Image)

들어가며

  • @Html2Image@ 패키지를 이용하여 간단하게 HTML 코드 이미지로 변환할 수 있다.

Html2Image Logo

 

방법

Html2Image 패키지 설치하기

> pip install html2image

 

예제 코드

  • 다음과 같이 3가지 방법으로 HTML 코드 이미지 파일로 만들 수 있다.
    • HTML 문자열(String) → 이미지
    • HTML 파일(@.html@) → 이미지
    • URL → 이미지
from html2image import Html2Image
hti = Html2Image()

html = '<h1> A title </h1> Some text.'
css = 'body {background: red;}'

# 1. screenshot an HTML string (css is optional)
hti.screenshot(html_str=html, css_str=css, save_as='page.png')

# 2. screenshot an HTML file
hti.screenshot(html_file='page.html', css_file='style.css', save_as='page2.png')

# 3. screenshot an URL
hti.screenshot(url='https://www.python.org', save_as='python_org.png')

 

 

참고 사이트

 

HTML to IMAGE using Python

Here is a variable html_str, it is a string that contains html tags and contents in body. I am created a .html file from this string using the below code in python. html_file = open("filename.html...

stackoverflow.com

 

파이썬 이미지캡쳐 모듈 (Html2Image)

from flask import Flask, send_from_directory, send_file from html2image import Html2Image app = Flask(__name__) @app.route("/") def hello_world(): url = 'https://www.python.org' width = 1200 height = 1024 filename = 'download_img.png' hti = Html2Image() #

gebal.tistory.com

 

html2image

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.

pypi.org

 

728x90