Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,18 +8,16 @@ from PIL import Image
|
|
| 8 |
import time
|
| 9 |
import base64
|
| 10 |
|
| 11 |
-
|
| 12 |
-
def take_screenshot_from_html(html_code, wait_time=3):
|
| 13 |
"""
|
| 14 |
HTMLコードからフルページスクリーンショットを取得する関数。
|
| 15 |
-
キャプチャ前に高さを3%拡張し、スクロールバーの有無に応じて調整。
|
| 16 |
|
| 17 |
Args:
|
| 18 |
html_code (str): スクリーンショットを取得するHTMLコード。
|
| 19 |
-
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
-
tuple: (PIL.Image.Image, str) スクリーンショット画像とメッセージ。
|
| 23 |
エラーの場合は (None, str) を返す。
|
| 24 |
"""
|
| 25 |
options = Options()
|
|
@@ -29,73 +27,65 @@ def take_screenshot_from_html(html_code, wait_time=3):
|
|
| 29 |
options.add_argument("--disable-gpu")
|
| 30 |
options.add_argument("--window-position=-2400,-2400") # Windowsでのバグ回避
|
| 31 |
|
| 32 |
-
driver = None # driverをtryの外で初期化
|
| 33 |
-
|
| 34 |
try:
|
| 35 |
driver = webdriver.Chrome(options=options)
|
| 36 |
html_base64 = base64.b64encode(html_code.encode('utf-8')).decode('utf-8')
|
| 37 |
driver.get(f"data:text/html;base64,{html_base64}")
|
| 38 |
|
| 39 |
-
time.sleep(
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
has_scrollbar = driver.execute_script(
|
| 43 |
-
"return document.documentElement.scrollHeight > document.documentElement.clientHeight;"
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# ページサイズを取得
|
| 47 |
width = driver.execute_script(
|
| 48 |
"return Math.max(document.body.scrollWidth, document.body.offsetWidth, "
|
| 49 |
"document.documentElement.clientWidth, document.documentElement.scrollWidth, "
|
| 50 |
"document.documentElement.offsetWidth);"
|
| 51 |
)
|
| 52 |
-
|
| 53 |
"return Math.max(document.body.scrollHeight, document.body.offsetHeight, "
|
| 54 |
"document.documentElement.clientHeight, document.documentElement.scrollHeight, "
|
| 55 |
"document.documentElement.offsetHeight);"
|
| 56 |
)
|
| 57 |
|
| 58 |
-
# 高さを
|
| 59 |
-
|
| 60 |
-
if has_scrollbar:
|
| 61 |
-
final_height = min(height + extra_height + 20, 5000) # スクロールバー分を考慮、最大5000px
|
| 62 |
-
else:
|
| 63 |
-
final_height = min(height + extra_height, 5000) # スクロールバーがなければ3%のみ、最大5000px
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
time.sleep(1)
|
| 67 |
|
|
|
|
| 68 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp:
|
| 69 |
temp_filename = temp.name
|
| 70 |
driver.save_screenshot(temp_filename)
|
|
|
|
|
|
|
| 71 |
image = Image.open(temp_filename)
|
| 72 |
os.unlink(temp_filename)
|
| 73 |
return image, "スクリーンショットを取得しました。"
|
| 74 |
|
| 75 |
except (WebDriverException, TimeoutException) as e:
|
|
|
|
| 76 |
return None, f"エラー: {str(e)}"
|
| 77 |
except Exception as e:
|
|
|
|
| 78 |
return None, f"予期しないエラー: {str(e)}"
|
| 79 |
-
finally:
|
| 80 |
-
if driver: # driverが存在する場合のみquit()を実行
|
| 81 |
-
driver.quit()
|
| 82 |
|
| 83 |
|
| 84 |
-
# Gradioインターフェース
|
| 85 |
with gr.Blocks(title="HTML Screenshot Tool") as demo:
|
| 86 |
gr.Markdown("# HTML スクリーンショットツール")
|
| 87 |
gr.Markdown("HTMLコードを入力して、レンダリングされたページのフルスクリーンショットを取得します。")
|
| 88 |
|
| 89 |
html_input = gr.Textbox(label="HTMLコード", placeholder="<p>Hello, world!</p>", lines=5)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
take_screenshot_from_html,
|
| 97 |
-
inputs=[html_input,
|
| 98 |
-
outputs=[
|
| 99 |
)
|
| 100 |
|
| 101 |
demo.launch()
|
|
|
|
| 8 |
import time
|
| 9 |
import base64
|
| 10 |
|
| 11 |
+
def take_screenshot_from_html(html_code, height_adjustment_percent=3):
|
|
|
|
| 12 |
"""
|
| 13 |
HTMLコードからフルページスクリーンショットを取得する関数。
|
|
|
|
| 14 |
|
| 15 |
Args:
|
| 16 |
html_code (str): スクリーンショットを取得するHTMLコード。
|
| 17 |
+
height_adjustment_percent (float): 高さの調整率(パーセント)。
|
| 18 |
|
| 19 |
Returns:
|
| 20 |
+
tuple: (PIL.Image.Image, str) スクリーンショット画像とメッセージのタプル。
|
| 21 |
エラーの場合は (None, str) を返す。
|
| 22 |
"""
|
| 23 |
options = Options()
|
|
|
|
| 27 |
options.add_argument("--disable-gpu")
|
| 28 |
options.add_argument("--window-position=-2400,-2400") # Windowsでのバグ回避
|
| 29 |
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
driver = webdriver.Chrome(options=options)
|
| 32 |
html_base64 = base64.b64encode(html_code.encode('utf-8')).decode('utf-8')
|
| 33 |
driver.get(f"data:text/html;base64,{html_base64}")
|
| 34 |
|
| 35 |
+
time.sleep(3) # 固定の待機時間 (3秒)
|
| 36 |
|
| 37 |
+
# フルページサイズを取得 (JavaScript)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
width = driver.execute_script(
|
| 39 |
"return Math.max(document.body.scrollWidth, document.body.offsetWidth, "
|
| 40 |
"document.documentElement.clientWidth, document.documentElement.scrollWidth, "
|
| 41 |
"document.documentElement.offsetWidth);"
|
| 42 |
)
|
| 43 |
+
original_height = driver.execute_script(
|
| 44 |
"return Math.max(document.body.scrollHeight, document.body.offsetHeight, "
|
| 45 |
"document.documentElement.clientHeight, document.documentElement.scrollHeight, "
|
| 46 |
"document.documentElement.offsetHeight);"
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# 高さを調整
|
| 50 |
+
adjusted_height = int(original_height * (1 + height_adjustment_percent / 100))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# ウィンドウサイズを設定(最大サイズ制限付き)
|
| 53 |
+
driver.set_window_size(min(width + 100, 1920), min(adjusted_height + 100, 5000))
|
| 54 |
time.sleep(1)
|
| 55 |
|
| 56 |
+
# スクリーンショットを一時ファイルに保存
|
| 57 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp:
|
| 58 |
temp_filename = temp.name
|
| 59 |
driver.save_screenshot(temp_filename)
|
| 60 |
+
|
| 61 |
+
driver.quit()
|
| 62 |
image = Image.open(temp_filename)
|
| 63 |
os.unlink(temp_filename)
|
| 64 |
return image, "スクリーンショットを取得しました。"
|
| 65 |
|
| 66 |
except (WebDriverException, TimeoutException) as e:
|
| 67 |
+
driver.quit()
|
| 68 |
return None, f"エラー: {str(e)}"
|
| 69 |
except Exception as e:
|
| 70 |
+
driver.quit()
|
| 71 |
return None, f"予期しないエラー: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
+
# Gradioインターフェース
|
| 75 |
with gr.Blocks(title="HTML Screenshot Tool") as demo:
|
| 76 |
gr.Markdown("# HTML スクリーンショットツール")
|
| 77 |
gr.Markdown("HTMLコードを入力して、レンダリングされたページのフルスクリーンショットを取得します。")
|
| 78 |
|
| 79 |
html_input = gr.Textbox(label="HTMLコード", placeholder="<p>Hello, world!</p>", lines=5)
|
| 80 |
+
height_adjustment_slider = gr.Slider(minimum=0, maximum=10, value=3, step=0.1, label="高さ調整率 (%)")
|
| 81 |
+
html_button = gr.Button("スクリーンショットを取得")
|
| 82 |
+
html_image_out = gr.Image(label="スクリーンショット")
|
| 83 |
+
html_text_out = gr.Textbox(label="結果")
|
| 84 |
|
| 85 |
+
html_button.click(
|
| 86 |
take_screenshot_from_html,
|
| 87 |
+
inputs=[html_input, height_adjustment_slider],
|
| 88 |
+
outputs=[html_image_out, html_text_out]
|
| 89 |
)
|
| 90 |
|
| 91 |
demo.launch()
|