Skip to content

Instantly share code, notes, and snippets.

@mjordan
Last active October 2, 2024 04:12
Show Gist options
  • Save mjordan/320befe368809e172f142426c50eee64 to your computer and use it in GitHub Desktop.
Save mjordan/320befe368809e172f142426c50eee64 to your computer and use it in GitHub Desktop.
'''
Script to generate tiles for an image viewed using the Mirador Viewer. Also takes
a screenshot of the page for quick QA of results.
Usage: python iiif_tile_warmer.py https://digital-ds.lib.sfu.ca/node/235 10 # Where 10 is the number of seconds to wait before closig the window; default is 5.
python iiif_tile_warmer.py tiles.txt 10 # Where tiles.txt is a file containing a list of URLs, one per line.
'''
import sys
import os
from time import sleep
import re
import logging
from selenium import webdriver
log_file_path = 'generate_iiif_tiles.log'
# screenshots_dir_path must exist.
screenshots_dir_path = '/tmp/screenshots'
if len(sys.argv) == 3:
sleep_length = sys.argv[2].strip()
else:
sleep_length = 5
logging.basicConfig(
filename=log_file_path,
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
)
def warm_tiles(url, sleep_length):
screenshot_filename = re.sub('[^0-9a-zA-Z]+', '_', url)
logging.info(f'Generating IIIF tiles for {url}, Chrome is waiting {sleep_length} seconds.')
try:
driver = webdriver.Chrome()
driver.get(url)
sleep(sleep_length)
driver.save_screenshot(os.path.join(screenshots_dir_path, screenshot_filename + '.png'))
driver.quit()
except Exception as e:
logging.error(f'Attempt to generate IIIF tiles for {url_to_warm} encountered an error: {e}')
# Main script logic.
url_to_warm = sys.argv[1].strip()
if url_to_warm.startswith('http'):
warm_tiles(url_to_warm, sleep_length)
else:
with open(url_to_warm) as fh:
for line in fh:
warm_tiles(line.strip(), sleep_length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment