Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,34 +7,42 @@ import os
|
|
| 7 |
from PIL import Image
|
| 8 |
import time
|
| 9 |
import base64
|
|
|
|
| 10 |
|
| 11 |
-
def take_screenshot_from_html(html_code, height_adjustment_percent=3):
|
| 12 |
-
"""
|
| 13 |
-
HTMLコードからフルページスクリーンショットを取得する関数。
|
| 14 |
-
|
| 15 |
-
Args:
|
| 16 |
-
html_code (str): スクリーンショットを取得するHTMLコード。
|
| 17 |
-
height_adjustment_percent (float): 高さの調整率(パーセント)。
|
| 18 |
|
| 19 |
-
|
| 20 |
-
tuple: (PIL.Image.Image, str) スクリーンショット画像とメッセージのタプル。
|
| 21 |
-
エラーの場合は (None, str) を返す。
|
| 22 |
-
"""
|
| 23 |
options = Options()
|
| 24 |
options.add_argument("--headless=new")
|
| 25 |
options.add_argument("--no-sandbox")
|
| 26 |
options.add_argument("--disable-dev-shm-usage")
|
| 27 |
options.add_argument("--disable-gpu")
|
| 28 |
-
options.add_argument("--window-position=-2400,-2400")
|
| 29 |
|
| 30 |
try:
|
| 31 |
driver = webdriver.Chrome(options=options)
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
driver.get(f"data:text/html;base64,{html_base64}")
|
| 34 |
|
| 35 |
-
time.sleep(3)
|
| 36 |
|
| 37 |
-
# フルページサイズを取得 (JavaScript)
|
| 38 |
width = driver.execute_script(
|
| 39 |
"return Math.max(document.body.scrollWidth, document.body.offsetWidth, "
|
| 40 |
"document.documentElement.clientWidth, document.documentElement.scrollWidth, "
|
|
@@ -46,14 +54,10 @@ def take_screenshot_from_html(html_code, height_adjustment_percent=3):
|
|
| 46 |
"document.documentElement.offsetHeight);"
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# 高さを調整
|
| 50 |
adjusted_height = int(original_height * (1 + height_adjustment_percent / 100))
|
| 51 |
-
|
| 52 |
-
# ウィンドウサイズを設定(最大サイズ制限付き)
|
| 53 |
driver.set_window_size(min(width + 100, 1920), min(adjusted_height + 100, 5000))
|
| 54 |
time.sleep(1)
|
| 55 |
|
| 56 |
-
# スクリーンショットを一時ファイルに保存
|
| 57 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp:
|
| 58 |
temp_filename = temp.name
|
| 59 |
driver.save_screenshot(temp_filename)
|
|
@@ -71,7 +75,7 @@ def take_screenshot_from_html(html_code, height_adjustment_percent=3):
|
|
| 71 |
return None, f"予期しないエラー: {str(e)}"
|
| 72 |
|
| 73 |
|
| 74 |
-
|
| 75 |
with gr.Blocks(title="HTML Screenshot Tool") as demo:
|
| 76 |
gr.Markdown("# HTML スクリーンショットツール")
|
| 77 |
gr.Markdown("HTMLコードを入力して、レンダリングされたページのフルスクリーンショットを取得します。")
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
import time
|
| 9 |
import base64
|
| 10 |
+
from bs4 import BeautifulSoup # HTML解析用
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def take_screenshot_from_html(html_code, height_adjustment_percent=3):
|
|
|
|
|
|
|
|
|
|
| 14 |
options = Options()
|
| 15 |
options.add_argument("--headless=new")
|
| 16 |
options.add_argument("--no-sandbox")
|
| 17 |
options.add_argument("--disable-dev-shm-usage")
|
| 18 |
options.add_argument("--disable-gpu")
|
| 19 |
+
options.add_argument("--window-position=-2400,-2400")
|
| 20 |
|
| 21 |
try:
|
| 22 |
driver = webdriver.Chrome(options=options)
|
| 23 |
+
|
| 24 |
+
# BeautifulSoupでHTMLを解析
|
| 25 |
+
soup = BeautifulSoup(html_code, 'html.parser')
|
| 26 |
+
|
| 27 |
+
# <head>タグがあるか確認し、なければ追加
|
| 28 |
+
if not soup.head:
|
| 29 |
+
head = soup.new_tag("head")
|
| 30 |
+
soup.insert(0, head)
|
| 31 |
+
|
| 32 |
+
# <meta charset>タグがあるか確認し、なければ追加(UTF-8を指定)
|
| 33 |
+
if not soup.head.find("meta", charset=True):
|
| 34 |
+
meta_charset = soup.new_tag("meta")
|
| 35 |
+
meta_charset["charset"] = "UTF-8"
|
| 36 |
+
soup.head.insert(0, meta_charset)
|
| 37 |
+
|
| 38 |
+
# 修正したHTMLを文字列に戻す
|
| 39 |
+
modified_html = str(soup)
|
| 40 |
+
|
| 41 |
+
html_base64 = base64.b64encode(modified_html.encode('utf-8')).decode('utf-8')
|
| 42 |
driver.get(f"data:text/html;base64,{html_base64}")
|
| 43 |
|
| 44 |
+
time.sleep(3)
|
| 45 |
|
|
|
|
| 46 |
width = driver.execute_script(
|
| 47 |
"return Math.max(document.body.scrollWidth, document.body.offsetWidth, "
|
| 48 |
"document.documentElement.clientWidth, document.documentElement.scrollWidth, "
|
|
|
|
| 54 |
"document.documentElement.offsetHeight);"
|
| 55 |
)
|
| 56 |
|
|
|
|
| 57 |
adjusted_height = int(original_height * (1 + height_adjustment_percent / 100))
|
|
|
|
|
|
|
| 58 |
driver.set_window_size(min(width + 100, 1920), min(adjusted_height + 100, 5000))
|
| 59 |
time.sleep(1)
|
| 60 |
|
|
|
|
| 61 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp:
|
| 62 |
temp_filename = temp.name
|
| 63 |
driver.save_screenshot(temp_filename)
|
|
|
|
| 75 |
return None, f"予期しないエラー: {str(e)}"
|
| 76 |
|
| 77 |
|
| 78 |
+
|
| 79 |
with gr.Blocks(title="HTML Screenshot Tool") as demo:
|
| 80 |
gr.Markdown("# HTML スクリーンショットツール")
|
| 81 |
gr.Markdown("HTMLコードを入力して、レンダリングされたページのフルスクリーンショットを取得します。")
|