Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,14 @@ import tempfile
|
|
| 10 |
import time
|
| 11 |
import os
|
| 12 |
|
| 13 |
-
def
|
| 14 |
# 1) 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 |
-
# 2) ヘッドレスChrome(Chromium)
|
| 21 |
options = Options()
|
| 22 |
options.add_argument("--headless")
|
| 23 |
options.add_argument("--no-sandbox")
|
|
@@ -27,54 +27,53 @@ def render_fullpage_screenshot_with_margin(html_code):
|
|
| 27 |
try:
|
| 28 |
driver = webdriver.Chrome(options=options)
|
| 29 |
|
| 30 |
-
# 3)
|
| 31 |
driver.set_window_size(1200, 800)
|
| 32 |
driver.get("file://" + tmp_path)
|
| 33 |
|
| 34 |
-
# 4)
|
| 35 |
WebDriverWait(driver, 10).until(
|
| 36 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 37 |
)
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
# 5)
|
| 41 |
driver.execute_script(
|
| 42 |
-
""
|
| 43 |
-
document.
|
| 44 |
-
document.body.style.overflow = 'hidden';
|
| 45 |
-
document.documentElement.style.backgroundColor = 'white';
|
| 46 |
-
document.body.style.backgroundColor = 'white';
|
| 47 |
-
"""
|
| 48 |
)
|
| 49 |
|
| 50 |
-
# 6)
|
| 51 |
scroll_width = driver.execute_script(
|
| 52 |
-
"return Math.max(
|
|
|
|
| 53 |
)
|
| 54 |
scroll_height = driver.execute_script(
|
| 55 |
-
"return Math.max(
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
# 7)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
# 8) 余白込みのウィンドウサイズに再設定
|
| 64 |
driver.set_window_size(final_width, final_height)
|
| 65 |
-
time.sleep(2) #
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
offset_y = int(scroll_height * margin_factor)
|
| 70 |
-
driver.execute_script(f"window.scrollTo({offset_x}, {offset_y});")
|
| 71 |
time.sleep(1)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
png = driver.get_screenshot_as_png()
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
-
#
|
| 78 |
return Image.new('RGB', (1, 1), color=(0, 0, 0))
|
| 79 |
|
| 80 |
finally:
|
|
@@ -82,16 +81,16 @@ def render_fullpage_screenshot_with_margin(html_code):
|
|
| 82 |
if os.path.exists(tmp_path):
|
| 83 |
os.remove(tmp_path)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
return Image.open(BytesIO(png))
|
| 87 |
|
| 88 |
-
# Gradio
|
| 89 |
iface = gr.Interface(
|
| 90 |
-
fn=
|
| 91 |
inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
|
| 92 |
-
outputs=gr.Image(type="pil", label="
|
| 93 |
-
title="Full Page
|
| 94 |
-
description="HTML
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
|
|
|
| 10 |
import time
|
| 11 |
import os
|
| 12 |
|
| 13 |
+
def render_fullpage_screenshot(html_code):
|
| 14 |
# 1) 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 |
+
# 2) ヘッドレスChrome(Chromium)起動オプション
|
| 21 |
options = Options()
|
| 22 |
options.add_argument("--headless")
|
| 23 |
options.add_argument("--no-sandbox")
|
|
|
|
| 27 |
try:
|
| 28 |
driver = webdriver.Chrome(options=options)
|
| 29 |
|
| 30 |
+
# 3) まずは適当なウィンドウサイズでページを開く
|
| 31 |
driver.set_window_size(1200, 800)
|
| 32 |
driver.get("file://" + tmp_path)
|
| 33 |
|
| 34 |
+
# 4) ページのロードを待機
|
| 35 |
WebDriverWait(driver, 10).until(
|
| 36 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 37 |
)
|
| 38 |
+
# 外部リソース読み込み等の安定化のため余分に待機
|
| 39 |
+
time.sleep(2)
|
| 40 |
|
| 41 |
+
# 5) スクロールバーを写したくない場合はCSSで非表示にする
|
| 42 |
driver.execute_script(
|
| 43 |
+
"document.documentElement.style.overflow = 'hidden';"
|
| 44 |
+
"document.body.style.overflow = 'hidden';"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# 6) ページ全体の幅・高さを正確に取得
|
| 48 |
scroll_width = driver.execute_script(
|
| 49 |
+
"return Math.max("
|
| 50 |
+
"document.body.scrollWidth, document.documentElement.scrollWidth)"
|
| 51 |
)
|
| 52 |
scroll_height = driver.execute_script(
|
| 53 |
+
"return Math.max("
|
| 54 |
+
"document.body.scrollHeight, document.documentElement.scrollHeight)"
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# 7) ウィンドウサイズに2%のマージンを加える
|
| 58 |
+
# 端が切れやすい場合に、余白をつけて確実に表示領域を広げる
|
| 59 |
+
margin_width = int(scroll_width * 0.02) # 幅の2%
|
| 60 |
+
margin_height = int(scroll_height * 0.02) # 高さの2%
|
| 61 |
+
|
| 62 |
+
final_width = scroll_width + margin_width
|
| 63 |
+
final_height = scroll_height + margin_height
|
| 64 |
|
|
|
|
| 65 |
driver.set_window_size(final_width, final_height)
|
| 66 |
+
time.sleep(2) # レイアウト再計算待ち
|
| 67 |
|
| 68 |
+
# 念のためページ最上部にスクロール
|
| 69 |
+
driver.execute_script("window.scrollTo(0, 0)")
|
|
|
|
|
|
|
| 70 |
time.sleep(1)
|
| 71 |
|
| 72 |
+
# 8) スクリーンショットを取得
|
| 73 |
png = driver.get_screenshot_as_png()
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
+
# 失敗時は1x1の黒画像を返す
|
| 77 |
return Image.new('RGB', (1, 1), color=(0, 0, 0))
|
| 78 |
|
| 79 |
finally:
|
|
|
|
| 81 |
if os.path.exists(tmp_path):
|
| 82 |
os.remove(tmp_path)
|
| 83 |
|
| 84 |
+
# 9) PNGバイナリをPIL.Imageに変換して返す
|
| 85 |
return Image.open(BytesIO(png))
|
| 86 |
|
| 87 |
+
# Gradioインターフェース
|
| 88 |
iface = gr.Interface(
|
| 89 |
+
fn=render_fullpage_screenshot,
|
| 90 |
inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
|
| 91 |
+
outputs=gr.Image(type="pil", label="ページ全体のスクリーンショット"),
|
| 92 |
+
title="Full Page Screenshot App",
|
| 93 |
+
description="HTMLをヘッドレスブラウザでレンダリングし、ページ全体を1枚の画像として取得します(端に2%の余裕)。"
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|