tomo2chin2 commited on
Commit
e0538f9
·
verified ·
1 Parent(s): b26e489

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -1,40 +1,62 @@
1
  import gradio as gr
2
  from selenium import webdriver
3
- from selenium.common.exceptions import WebDriverException
 
 
 
4
  from PIL import Image
5
  from io import BytesIO
6
- import tempfile, os
 
 
7
 
8
  def render_html_to_image(html_code):
9
- # 入力HTMLを一時ファイルに保存
10
  tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
11
  tmp_path = tmp_file.name
12
  tmp_file.write(html_code.encode('utf-8'))
13
  tmp_file.close()
14
-
15
- # ChromeChromium)をヘッドレスモードで起動
16
- options = webdriver.ChromeOptions()
17
- options.add_argument('--headless') # ヘッドレス(画面非表示)モード
18
- options.add_argument('--no-sandbox') # コンテナ環境でのサンドボックス無効化
19
- options.add_argument('--disable-dev-shm-usage') # /dev/shm の共有メモリ領域を不使用
 
 
20
  try:
 
21
  driver = webdriver.Chrome(options=options)
22
- driver.set_window_size(1200, 800) # ウィンドウサイズを適宜指定
23
- # ローカルHTMLファイルを開く(file:// スキームを使用)
 
 
24
  driver.get("file://" + tmp_path)
25
- driver.implicitly_wait(3) # レンダリング待ち (最大3秒)
 
 
 
 
 
 
 
 
 
26
  png = driver.get_screenshot_as_png()
27
- except WebDriverException as e:
28
- # 失敗時は1x1の透明画像を返すなどの処理
29
- return Image.new('RGB', (1, 1), color=(0,0,0))
 
 
30
  finally:
31
  driver.quit()
32
  if os.path.exists(tmp_path):
33
  os.remove(tmp_path)
34
- # PillowでPNGバイナリからImageオブジェクトを生成
 
35
  return Image.open(BytesIO(png))
36
 
37
- # Gradioインタフェースの定義
38
  iface = gr.Interface(
39
  fn=render_html_to_image,
40
  inputs=gr.Textbox(lines=15, label="HTMLコード入力"),
 
1
  import gradio as gr
2
  from selenium import webdriver
3
+ from selenium.webdriver.chrome.options import Options
4
+ from selenium.webdriver.common.by import By
5
+ from selenium.webdriver.support.ui import WebDriverWait
6
+ from selenium.webdriver.support import expected_conditions as EC
7
  from PIL import Image
8
  from io import BytesIO
9
+ import tempfile
10
+ import time
11
+ import os
12
 
13
  def render_html_to_image(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(Chromium)起動オプションを設定
21
+ options = Options()
22
+ options.add_argument("--headless") # ヘッドレスモード
23
+ options.add_argument("--no-sandbox") # コンテナ環境でのサンドボックス無効化
24
+ options.add_argument("--disable-dev-shm-usage") # /dev/shmを使わない
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
+ # ローカルHTMLを開く
34
  driver.get("file://" + tmp_path)
35
+
36
+ # ---- 待機処理 (1) 明示的待機で「body」タグの読み込み完了を待つ ----
37
+ WebDriverWait(driver, 10).until(
38
+ EC.presence_of_element_located((By.TAG_NAME, "body"))
39
+ )
40
+
41
+ # ---- 待機処理 (2) 追加のスリープでフォント等の読み込みをさらに確実化 ----
42
+ time.sleep(2)
43
+
44
+ # スクリーンショットをPNGバイナリとして取得
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:
52
  driver.quit()
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=render_html_to_image,
62
  inputs=gr.Textbox(lines=15, label="HTMLコード入力"),