Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,70 +11,65 @@ import time
|
|
| 11 |
import os
|
| 12 |
|
| 13 |
def render_fullpage_screenshot(html_code):
|
| 14 |
-
"""
|
| 15 |
-
ページを少しずつスクロールしながら複数回のスクリーンショットを撮影し、
|
| 16 |
-
それらを縦方向に結合して1枚の長い画像にする。
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
-
# 1) HTMLコードを一時ファイルに保存
|
| 20 |
tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 21 |
tmp_path = tmp_file.name
|
| 22 |
tmp_file.write(html_code.encode('utf-8'))
|
| 23 |
tmp_file.close()
|
| 24 |
|
| 25 |
-
# 2) ヘッドレスChrome(Chromium)起動オプション設定
|
| 26 |
options = Options()
|
| 27 |
-
options.add_argument("--headless")
|
| 28 |
-
options.add_argument("--no-sandbox")
|
| 29 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 30 |
-
options.add_argument("--force-device-scale-factor=1")
|
| 31 |
|
| 32 |
try:
|
| 33 |
driver = webdriver.Chrome(options=options)
|
| 34 |
-
# 適当な初期ウィンドウサイズに設定してページを読み込む
|
| 35 |
driver.set_window_size(1200, 800)
|
| 36 |
driver.get("file://" + tmp_path)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
WebDriverWait(driver, 10).until(
|
| 40 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 41 |
)
|
| 42 |
-
# さらにフォントや外部リソースを読み込み終わるまで少し待つ
|
| 43 |
time.sleep(2)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
# (ただし一部ページでレイアウトが変化する可能性があるので要注意)
|
| 47 |
driver.execute_script(
|
| 48 |
-
"
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
time.sleep(1)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
viewport_height = driver.execute_script("return window.innerHeight")
|
| 55 |
scroll_height = driver.execute_script("return document.body.scrollHeight")
|
| 56 |
|
| 57 |
-
# スクロール+キャプチャを繰り返す
|
| 58 |
images = []
|
| 59 |
current_position = 0
|
| 60 |
|
| 61 |
while True:
|
| 62 |
-
#
|
| 63 |
png = driver.get_screenshot_as_png()
|
| 64 |
img = Image.open(BytesIO(png))
|
| 65 |
images.append(img)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
if current_position + viewport_height >= scroll_height:
|
| 69 |
break
|
| 70 |
|
| 71 |
# 次のスクロール位置へ移動
|
| 72 |
current_position += viewport_height
|
| 73 |
driver.execute_script(f"window.scrollTo(0, {current_position})")
|
| 74 |
-
# スクロール後の描画待ち
|
| 75 |
time.sleep(1)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
total_width = max(img.width for img in images)
|
| 79 |
total_height = sum(img.height for img in images)
|
| 80 |
full_screenshot = Image.new('RGB', (total_width, total_height))
|
|
@@ -85,7 +80,6 @@ def render_fullpage_screenshot(html_code):
|
|
| 85 |
current_y += img.height
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
-
# 何らかのエラーが発生した場合、1x1の黒画像を返す
|
| 89 |
return Image.new('RGB', (1, 1), color=(0, 0, 0))
|
| 90 |
|
| 91 |
finally:
|
|
@@ -95,16 +89,12 @@ def render_fullpage_screenshot(html_code):
|
|
| 95 |
|
| 96 |
return full_screenshot
|
| 97 |
|
| 98 |
-
# Gradioインターフェース
|
| 99 |
iface = gr.Interface(
|
| 100 |
fn=render_fullpage_screenshot,
|
| 101 |
inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
|
| 102 |
outputs=gr.Image(type="pil", label="フルページスクリーンショット"),
|
| 103 |
-
title="
|
| 104 |
-
description=
|
| 105 |
-
"ページを少しずつスクロールしながら複数回キャプチャを撮影し、"
|
| 106 |
-
"最終的に縦に結合して1枚の長い画像を生成します。"
|
| 107 |
-
)
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
|
|
|
| 11 |
import os
|
| 12 |
|
| 13 |
def render_fullpage_screenshot(html_code):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 15 |
tmp_path = tmp_file.name
|
| 16 |
tmp_file.write(html_code.encode('utf-8'))
|
| 17 |
tmp_file.close()
|
| 18 |
|
|
|
|
| 19 |
options = Options()
|
| 20 |
+
options.add_argument("--headless")
|
| 21 |
+
options.add_argument("--no-sandbox")
|
| 22 |
+
options.add_argument("--disable-dev-shm-usage")
|
| 23 |
+
options.add_argument("--force-device-scale-factor=1")
|
| 24 |
|
| 25 |
try:
|
| 26 |
driver = webdriver.Chrome(options=options)
|
|
|
|
| 27 |
driver.set_window_size(1200, 800)
|
| 28 |
driver.get("file://" + tmp_path)
|
| 29 |
|
| 30 |
+
# ページロード待機
|
| 31 |
WebDriverWait(driver, 10).until(
|
| 32 |
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
| 33 |
)
|
|
|
|
| 34 |
time.sleep(2)
|
| 35 |
|
| 36 |
+
# 固定要素を強制的に「static」にして、重複しにくくする
|
|
|
|
| 37 |
driver.execute_script(
|
| 38 |
+
"""
|
| 39 |
+
const elems = document.querySelectorAll('*');
|
| 40 |
+
for (const e of elems) {
|
| 41 |
+
const style = window.getComputedStyle(e);
|
| 42 |
+
if (style.position === 'fixed' || style.position === 'sticky') {
|
| 43 |
+
e.style.position = 'static';
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
"""
|
| 47 |
)
|
| 48 |
time.sleep(1)
|
| 49 |
|
| 50 |
+
# ページ高さを取得
|
| 51 |
viewport_height = driver.execute_script("return window.innerHeight")
|
| 52 |
scroll_height = driver.execute_script("return document.body.scrollHeight")
|
| 53 |
|
|
|
|
| 54 |
images = []
|
| 55 |
current_position = 0
|
| 56 |
|
| 57 |
while True:
|
| 58 |
+
# スクリーンショット
|
| 59 |
png = driver.get_screenshot_as_png()
|
| 60 |
img = Image.open(BytesIO(png))
|
| 61 |
images.append(img)
|
| 62 |
|
| 63 |
+
# 最下部まで行ったら終了
|
| 64 |
if current_position + viewport_height >= scroll_height:
|
| 65 |
break
|
| 66 |
|
| 67 |
# 次のスクロール位置へ移動
|
| 68 |
current_position += viewport_height
|
| 69 |
driver.execute_script(f"window.scrollTo(0, {current_position})")
|
|
|
|
| 70 |
time.sleep(1)
|
| 71 |
|
| 72 |
+
# 縦方向に結合
|
| 73 |
total_width = max(img.width for img in images)
|
| 74 |
total_height = sum(img.height for img in images)
|
| 75 |
full_screenshot = Image.new('RGB', (total_width, total_height))
|
|
|
|
| 80 |
current_y += img.height
|
| 81 |
|
| 82 |
except Exception as e:
|
|
|
|
| 83 |
return Image.new('RGB', (1, 1), color=(0, 0, 0))
|
| 84 |
|
| 85 |
finally:
|
|
|
|
| 89 |
|
| 90 |
return full_screenshot
|
| 91 |
|
|
|
|
| 92 |
iface = gr.Interface(
|
| 93 |
fn=render_fullpage_screenshot,
|
| 94 |
inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
|
| 95 |
outputs=gr.Image(type="pil", label="フルページスクリーンショット"),
|
| 96 |
+
title="Scrolling Screenshot (Remove Fixed Elements)",
|
| 97 |
+
description="固定要素をstaticに変更して、重複を防ぎつつページ全体をキャプチャします。"
|
|
|
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|