tomo2chin2 commited on
Commit
630fc7d
·
verified ·
1 Parent(s): 61e735f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -53
app.py CHANGED
@@ -10,7 +10,7 @@ import tempfile
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
@@ -27,7 +27,7 @@ def render_fullpage_screenshot(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
 
@@ -35,59 +35,19 @@ def render_fullpage_screenshot(html_code):
35
  WebDriverWait(driver, 10).until(
36
  EC.presence_of_element_located((By.TAG_NAME, "body"))
37
  )
38
- time.sleep(2) # 外部リソース読み込みなどを待つ
39
 
40
- # 5) スクロールバーを写したくない場合はCSSで非表示に
41
- driver.execute_script(
42
- "document.documentElement.style.overflow = 'hidden';"
43
- "document.body.style.overflow = 'hidden';"
44
- )
 
 
45
 
46
- # 6) ページ全体の幅・高さを取得 (body documentElement の大きい方を使用)
47
  scroll_width = driver.execute_script(
48
- "return Math.max("
49
- "document.body.scrollWidth, document.documentElement.scrollWidth)"
50
  )
51
  scroll_height = driver.execute_script(
52
- "return Math.max("
53
- "document.body.scrollHeight, document.documentElement.scrollHeight)"
54
- )
55
-
56
- # 7) ウィンドウサイズをページ全体 + 5%上下左右マージン (=幅・高さ共に10%拡大) に設定
57
- margin_factor = 0.10 # 上下左右それぞれ5%を足す => 合計で10%増やすイメージ
58
- adjusted_width = int(scroll_width * (1 + margin_factor))
59
- adjusted_height = int(scroll_height * (1 + margin_factor))
60
-
61
- driver.set_window_size(adjusted_width, adjusted_height)
62
- time.sleep(2) # レイアウトが変わるので少し待つ
63
-
64
- # 念のためページ最上部にスクロールしてからキャプチャ
65
- driver.execute_script("window.scrollTo(0, 0)")
66
- time.sleep(1)
67
-
68
- # 8) スクリーンショットを取得
69
- png = driver.get_screenshot_as_png()
70
-
71
- except Exception as e:
72
- # 失敗時は1x1の黒画像を返す
73
- return Image.new('RGB', (1, 1), color=(0, 0, 0))
74
-
75
- finally:
76
- driver.quit()
77
- if os.path.exists(tmp_path):
78
- os.remove(tmp_path)
79
-
80
- # 9) PNGバイナリをPIL.Imageに変換して返す
81
- return Image.open(BytesIO(png))
82
-
83
- # Gradioインターフェース
84
- iface = gr.Interface(
85
- fn=render_fullpage_screenshot,
86
- inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
87
- outputs=gr.Image(type="pil", label="ページ全体のスクリーンショット"),
88
- title="Full Page Screenshot with Margin",
89
- description="HTMLをヘッドレスブラウザでレンダリングし、ページ全体+5%のマージンをつけて1枚の画像として取得します。"
90
- )
91
-
92
- if __name__ == "__main__":
93
- iface.launch()
 
10
  import time
11
  import os
12
 
13
+ def render_fullpage_screenshot_with_margin(html_code):
14
  # 1) HTMLコードを一時ファイルに保存
15
  tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
16
  tmp_path = tmp_file.name
 
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
 
 
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) ページ全体の幅・高さを取得(bodyとdocumentElement両方を比較)
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