Skip to content

Instantly share code, notes, and snippets.

@ncaq
Last active April 16, 2020 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncaq/b84aa1abef2c8162f674c351504e7824 to your computer and use it in GitHub Desktop.
Save ncaq/b84aa1abef2c8162f674c351504e7824 to your computer and use it in GitHub Desktop.
MathJaxを含むローカルのHTMLファイルをpngに変換する
#!/usr/bin/env python3
import os
import subprocess
import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
options = Options()
options.add_argument('--headless')
options.add_argument('--force-device-scale-factor=5')
driver = webdriver.Chrome(options=options)
driver.get('file:///' + os.path.abspath(sys.argv[1]))
WebDriverWait(driver, 10, 0.5).until(
EC.visibility_of_element_located((By.ID, 'MathJax-Element-1-Frame')))
page_width = driver.execute_script(
'return document.documentElement.offsetWidth')
page_height = driver.execute_script(
'return document.documentElement.offsetHeight') + 1
driver.set_window_size(page_width, page_height)
output_path = 'screenshot.png'
driver.save_screenshot(output_path)
driver.quit()
subprocess.call(['mogrify', '-mattecolor', 'white',
'-trim', '-frame', '5x5', output_path])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment