Skip to content

Commit 53d4393

Browse files
committed
High res previews, fix lorentz
1 parent b039971 commit 53d4393

File tree

44 files changed

+24
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+24
-10
lines changed

capture_all_latex_previews.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
"""
2+
Script to capture screenshots of all the formulas using Selenium.
3+
This shit doesn't work; see https://stackoverflow.com/questions/76553944/
4+
"""
15
import os
26
import time
37

48
from selenium import webdriver
59
from selenium.webdriver.common.by import By
610
from selenium.webdriver.chrome.service import Service as ChromeService
7-
from selenium.webdriver.common.keys import Keys
811
from webdriver_manager.chrome import ChromeDriverManager
912
import pyautogui
10-
import PIL
11-
12-
dry_run = False
1313

14+
dry_run = False # if False, don't save anything
15+
save_to_temp_folder = True # if True, save to temp folder instead of content/formulas (for previewing)
1416
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
1517

16-
driver.get('http://localhost:1313/formulas')
18+
driver.get('http://stemformulas.com/formulas')
1719
driver.implicitly_wait(10)
1820

1921
def zoom_in(times=1):
@@ -29,11 +31,12 @@ def zoom_in(times=1):
2931
print(f"Found {page_count} pages of formulas")
3032

3133
# increase zoom to 400% for higher quality screenshots
32-
# zoom_in(8) # TODO, screenshots become weird when zoomed in, but we want this for higher quality screenshots
34+
# zoom_in(4)
35+
3336
for i in range(1, page_count + 1): # iterate over pages
3437
print("Visiting page: ", i)
3538
# visit formulas page
36-
driver.get(f'http://localhost:1313/formulas/page/{i}')
39+
driver.get(f'http://stemformulas.com/formulas/page/{i}')
3740

3841
# Find the section with class "grid-container mt-6"
3942
section = driver.find_element(By.CSS_SELECTOR, 'section.grid-container.mt-6')
@@ -53,16 +56,27 @@ def zoom_in(times=1):
5356
x_path = f"//*[@id=\"main-content\"]/section[2]/a[{i+1}]/div[1]"
5457

5558
div = anchor.find_element(By.XPATH, x_path)
56-
59+
div.location_once_scrolled_into_view # scroll into view
5760
# http://localhost:1313/formulas/<folder_name>/
5861
folder_name = href.split('/')[-2]
5962
output_image_path = os.path.join("content", "formulas", folder_name, "preview.png")
6063

6164
if dry_run:
6265
print(f"Would have screenshot div {div} to {output_image_path}")
66+
elif save_to_temp_folder:
67+
if not os.path.exists("temp"):
68+
os.mkdir("temp")
69+
if not os.path.exists(os.path.join("temp", folder_name)):
70+
os.mkdir(os.path.join("temp", folder_name))
71+
new_path = os.path.join("temp", folder_name, "preview.png")
72+
bits = div.screenshot_as_png
73+
with open(new_path, 'wb') as f:
74+
f.write(bits)
75+
print(f"Screenshot div {div} to {new_path}")
76+
time.sleep(1)
6377
else:
6478
bits = div.screenshot_as_png
6579
with open(output_image_path, 'wb') as f:
6680
f.write(bits)
6781
print(f"Screenshot div {div} to {output_image_path}")
68-
time.sleep(0.7) # if we don't sleep, elements become stale
82+
time.sleep(1) # if we don't sleep, elements become stale
46.9 KB
47.1 KB
81.9 KB
55.9 KB
71 KB
86.1 KB
34.7 KB
92.4 KB
49.8 KB

0 commit comments

Comments
 (0)