Spaces:
Paused
Paused
Update app.py
Browse files9 unmodified lines1011121314151610 unmodified lines272829303132331 unmodified line35363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939 unmodified linesimport timeimport osdef render_fullpage_screenshot(html_code):# 1) HTMLコードを一時ファイルに保存tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)tmp_path = tmp_file.name10 unmodified linestry:driver = webdriver.Chrome(options=options)# 3) とりあえずある程度のウィンドウサイズでページを開くdriver.set_window_size(1200, 800)driver.get("file://" + tmp_path)1 unmodified lineWebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))time.sleep(2) # 外部リソース読み込みなどを待つ# 5) スクロールバーを写したくない場合はCSSで非表示にdriver.execute_script("document.documentElement.style.overflow = 'hidden';""document.body.style.overflow = 'hidden';")# 6) ページ全体の幅・高さを取得 (body と documentElement の大きい方を使用)scroll_width = driver.execute_script("return Math.max(""document.body.scrollWidth, document.documentElement.scrollWidth)")scroll_height = driver.execute_script("return Math.max(""document.body.scrollHeight, document.documentElement.scrollHeight)")# 7) ウィンドウサイズをページ全体 + 5%上下左右マージン (=幅・高さ共に10%拡大) に設定margin_factor = 0.10 # 上下左右それぞれ5%を足す => 合計で10%増やすイメージadjusted_width = int(scroll_width * (1 + margin_factor))adjusted_height = int(scroll_height * (1 + margin_factor))driver.set_window_size(adjusted_width, adjusted_height)time.sleep(2) # レイアウトが変わるので少し待つ# 念のためページ最上部にスクロールしてからキャプチャdriver.execute_script("window.scrollTo(0, 0)")time.sleep(1)# 8) スクリーンショットを取得png = driver.get_screenshot_as_png()except Exception as e:# 失敗時は1x1の黒画像を返すreturn Image.new('RGB', (1, 1), color=(0, 0, 0))finally:driver.quit()if os.path.exists(tmp_path):os.remove(tmp_path)# 9) PNGバイナリをPIL.Imageに変換して返すreturn Image.open(BytesIO(png))# Gradioインターフェースiface = gr.Interface(fn=render_fullpage_screenshot,inputs=gr.Textbox(lines=15, label="HTMLコード入力"),outputs=gr.Image(type="pil", label="ページ全体のスクリーンショット"),title="Full Page Screenshot with Margin",description="HTMLをヘッドレスブラウザでレンダリングし、ページ全体+5%のマージンをつけて1枚の画像として取得します。")if __name__ == "__main__":iface.launch()9 unmodified lines1011121314151610 unmodified lines272829303132331 unmodified line353637383940414243444546474849505152539 unmodified linesimport timeimport osdef render_fullpage_screenshot_with_margin(html_code):# 1) HTMLコードを一時ファイルに保存tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)tmp_path = tmp_file.name10 unmodified linestry:driver = webdriver.Chrome(options=options)# 3) まずはある程度のウィンドウサイズでページを開くdriver.set_window_size(1200, 800)driver.get("file://" + tmp_path)1 unmodified lineWebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))time.sleep(2) # フォントや外部リソース読み込み安定化# 5) スクロールバーを非表示にし、背景を白に統一(必要に応じて変更)driver.execute_script("""document.documentElement.style.overflow = 'hidden';document.body.style.overflow = 'hidden';document.documentElement.style.backgroundColor = 'white';document.body.style.backgroundColor = 'white';""")# 6) ページ全体の幅・高さを取得(bodyとdocumentElement両方を比較)scroll_width = driver.execute_script("return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth)")scroll_height = driver.execute_script("return Math.max(document.body
app.py+13-53