Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ def render_fullpage_screenshot_with_margin(html_code):
|
|
| 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,27 +27,72 @@ 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 |
-
time.sleep(2) #
|
| 39 |
|
| 40 |
-
# 5)
|
| 41 |
-
driver.execute_script(
|
|
|
|
| 42 |
document.documentElement.style.overflow = 'hidden';
|
| 43 |
document.body.style.overflow = 'hidden';
|
| 44 |
document.documentElement.style.backgroundColor = 'white';
|
| 45 |
document.body.style.backgroundColor = 'white';
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
# 6)
|
| 49 |
scroll_width = driver.execute_script(
|
| 50 |
-
"return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth)"
|
| 51 |
)
|
| 52 |
scroll_height = driver.execute_script(
|
| 53 |
-
"return Math.max(document.body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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) ページのロード完了を待機(bodyタグの存在)
|
| 35 |
WebDriverWait(driver, 10).until(
|
| 36 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 37 |
)
|
| 38 |
+
time.sleep(2) # 外部リソースの読み込みを待つ
|
| 39 |
|
| 40 |
+
# 5) スクロールバー非表示と背景色の固定(マルチライン文字列で記述)
|
| 41 |
+
driver.execute_script(
|
| 42 |
+
"""
|
| 43 |
document.documentElement.style.overflow = 'hidden';
|
| 44 |
document.body.style.overflow = 'hidden';
|
| 45 |
document.documentElement.style.backgroundColor = 'white';
|
| 46 |
document.body.style.backgroundColor = 'white';
|
| 47 |
+
"""
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
# 6) ページ全体の幅と高さを、bodyとdocumentElementから取得(大きい方を使用)
|
| 51 |
scroll_width = driver.execute_script(
|
| 52 |
+
"return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth);"
|
| 53 |
)
|
| 54 |
scroll_height = driver.execute_script(
|
| 55 |
+
"return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# 7) 上下左右に5%余白を確保(合計10%拡大)
|
| 59 |
+
margin_factor = 0.05 # 5%
|
| 60 |
+
final_width = int(scroll_width * (1 + margin_factor * 2))
|
| 61 |
+
final_height = int(scroll_height * (1 + margin_factor * 2))
|
| 62 |
+
|
| 63 |
+
# 8) 余白込みのウィンドウサイズに再設定
|
| 64 |
+
driver.set_window_size(final_width, final_height)
|
| 65 |
+
time.sleep(2) # レイアウト変化の安定待ち
|
| 66 |
+
|
| 67 |
+
# 9) コンテンツを中央に配置するため、余白分だけスクロール
|
| 68 |
+
offset_x = int(scroll_width * margin_factor)
|
| 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 |
+
# 10) スクリーンショットをPNGバイナリとして取得
|
| 74 |
+
png = driver.get_screenshot_as_png()
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
# エラー発生時は1x1の黒画像を返す
|
| 78 |
+
return Image.new('RGB', (1, 1), color=(0, 0, 0))
|
| 79 |
+
|
| 80 |
+
finally:
|
| 81 |
+
driver.quit()
|
| 82 |
+
if os.path.exists(tmp_path):
|
| 83 |
+
os.remove(tmp_path)
|
| 84 |
+
|
| 85 |
+
# 11) PNGバイナリをPIL.Imageに変換して返す
|
| 86 |
+
return Image.open(BytesIO(png))
|
| 87 |
+
|
| 88 |
+
# Gradioインターフェースの定義
|
| 89 |
+
iface = gr.Interface(
|
| 90 |
+
fn=render_fullpage_screenshot_with_margin,
|
| 91 |
+
inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
|
| 92 |
+
outputs=gr.Image(type="pil", label="ページ全体 + 5%余白スクリーンショット"),
|
| 93 |
+
title="Full Page + Margin Screenshot App",
|
| 94 |
+
description="HTMLをヘッドレスブラウザでレンダリングし、上下左右に5%余白を含めて1枚の画像として取得します。"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
iface.launch()
|