Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,42 +10,52 @@ import tempfile
|
|
| 10 |
import time
|
| 11 |
import os
|
| 12 |
|
| 13 |
-
def
|
| 14 |
# HTMLコードを一時ファイルに保存
|
| 15 |
tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 16 |
tmp_path = tmp_file.name
|
| 17 |
tmp_file.write(html_code.encode('utf-8'))
|
| 18 |
tmp_file.close()
|
| 19 |
|
| 20 |
-
# ヘッドレスChrome
|
| 21 |
options = Options()
|
| 22 |
-
options.add_argument("--headless")
|
| 23 |
-
options.add_argument("--no-sandbox")
|
| 24 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 25 |
-
options.add_argument("--force-device-scale-factor=1")
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
# ブラウザ起動
|
| 29 |
driver = webdriver.Chrome(options=options)
|
| 30 |
-
# レンダリングする画面サイズを固定
|
| 31 |
-
driver.set_window_size(1200, 800)
|
| 32 |
|
| 33 |
-
#
|
|
|
|
| 34 |
driver.get("file://" + tmp_path)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
WebDriverWait(driver, 10).until(
|
| 38 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 39 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
time.sleep(2)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
png = driver.get_screenshot_as_png()
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
# 何らかのエラーが発生した場合は1x1ピクセルの黒画像を返すなどのfallback
|
| 49 |
return Image.new('RGB', (1, 1), color=(0, 0, 0))
|
| 50 |
|
| 51 |
finally:
|
|
@@ -53,16 +63,15 @@ def render_html_to_image(html_code):
|
|
| 53 |
if os.path.exists(tmp_path):
|
| 54 |
os.remove(tmp_path)
|
| 55 |
|
| 56 |
-
# PNGバイナリをPIL.Imageに変換して返す
|
| 57 |
return Image.open(BytesIO(png))
|
| 58 |
|
| 59 |
-
# Gradio
|
| 60 |
iface = gr.Interface(
|
| 61 |
-
fn=
|
| 62 |
inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
|
| 63 |
-
outputs=gr.Image(type="pil", label="
|
| 64 |
-
title="
|
| 65 |
-
description="
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 10 |
import time
|
| 11 |
import os
|
| 12 |
|
| 13 |
+
def render_fullpage_screenshot(html_code):
|
| 14 |
# HTMLコードを一時ファイルに保存
|
| 15 |
tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 16 |
tmp_path = tmp_file.name
|
| 17 |
tmp_file.write(html_code.encode('utf-8'))
|
| 18 |
tmp_file.close()
|
| 19 |
|
| 20 |
+
# ヘッドレスChrome起動オプション
|
| 21 |
options = Options()
|
| 22 |
+
options.add_argument("--headless")
|
| 23 |
+
options.add_argument("--no-sandbox")
|
| 24 |
+
options.add_argument("--disable-dev-shm-usage")
|
| 25 |
+
options.add_argument("--force-device-scale-factor=1")
|
| 26 |
|
| 27 |
try:
|
|
|
|
| 28 |
driver = webdriver.Chrome(options=options)
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# まずは適当なウィンドウサイズでページを読み込む
|
| 31 |
+
driver.set_window_size(1200, 800)
|
| 32 |
driver.get("file://" + tmp_path)
|
| 33 |
|
| 34 |
+
# ページロード完了を待つ
|
| 35 |
WebDriverWait(driver, 10).until(
|
| 36 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 37 |
)
|
| 38 |
+
# さらにフォントや外部リソース読み込みの安定化のため少し待機
|
| 39 |
+
time.sleep(2)
|
| 40 |
+
|
| 41 |
+
# ---- スクロールバーを非表示にする(必要なら)----
|
| 42 |
+
driver.execute_script(
|
| 43 |
+
"document.documentElement.style.overflow = 'hidden';"
|
| 44 |
+
"document.body.style.overflow = 'hidden';"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# ---- ページ全体の高さと幅を取得してウィンドウサイズを再設定 ----
|
| 48 |
+
scroll_width = driver.execute_script("return document.body.scrollWidth")
|
| 49 |
+
scroll_height = driver.execute_script("return document.body.scrollHeight")
|
| 50 |
|
| 51 |
+
driver.set_window_size(scroll_width, scroll_height)
|
| 52 |
+
# レイアウトが変わる可能性があるので再度待機
|
| 53 |
time.sleep(2)
|
| 54 |
|
| 55 |
+
# 最終的にページ全体をスクリーンショット
|
| 56 |
png = driver.get_screenshot_as_png()
|
| 57 |
|
| 58 |
except Exception as e:
|
|
|
|
| 59 |
return Image.new('RGB', (1, 1), color=(0, 0, 0))
|
| 60 |
|
| 61 |
finally:
|
|
|
|
| 63 |
if os.path.exists(tmp_path):
|
| 64 |
os.remove(tmp_path)
|
| 65 |
|
|
|
|
| 66 |
return Image.open(BytesIO(png))
|
| 67 |
|
| 68 |
+
# Gradioインターフェース
|
| 69 |
iface = gr.Interface(
|
| 70 |
+
fn=render_fullpage_screenshot,
|
| 71 |
inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
|
| 72 |
+
outputs=gr.Image(type="pil", label="ページ全体のスクリーンショット"),
|
| 73 |
+
title="Full Page Screenshot App",
|
| 74 |
+
description="HTMLをヘッドレスブラウザでレンダリングし、ページ全体を1枚の画像として取得します。"
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|