diff --git a/images/screenshot-dark-home.png b/images/screenshot-dark-home.png index 37cd06d..73e3903 100644 Binary files a/images/screenshot-dark-home.png and b/images/screenshot-dark-home.png differ diff --git a/images/screenshot-dark-list.png b/images/screenshot-dark-list.png index 572ed36..1518ae1 100644 Binary files a/images/screenshot-dark-list.png and b/images/screenshot-dark-list.png differ diff --git a/images/screenshot-dark-post.png b/images/screenshot-dark-post.png index 6990dc6..b066bcb 100644 Binary files a/images/screenshot-dark-post.png and b/images/screenshot-dark-post.png differ diff --git a/images/screenshot-light-home.png b/images/screenshot-light-home.png index 3a478ae..26b5ba9 100644 Binary files a/images/screenshot-light-home.png and b/images/screenshot-light-home.png differ diff --git a/images/screenshot-light-list.png b/images/screenshot-light-list.png index e8db3a9..b2fa894 100644 Binary files a/images/screenshot-light-list.png and b/images/screenshot-light-list.png differ diff --git a/images/screenshot-light-post.png b/images/screenshot-light-post.png index 00ac2ce..70bc602 100644 Binary files a/images/screenshot-light-post.png and b/images/screenshot-light-post.png differ diff --git a/images/screenshot.png b/images/screenshot.png index 3dd26a5..6707d66 100644 Binary files a/images/screenshot.png and b/images/screenshot.png differ diff --git a/images/tn.png b/images/tn.png index 3dd26a5..6707d66 100644 Binary files a/images/tn.png and b/images/tn.png differ diff --git a/screenshot.py b/screenshot.py new file mode 100644 index 0000000..7e9c3b5 --- /dev/null +++ b/screenshot.py @@ -0,0 +1,90 @@ +from selenium import webdriver +from selenium.webdriver.firefox.options import Options +from PIL import Image +import os + + +""" +Setup: + pip install selenium pillow + + Add geckodriver to $PATH. Link: https://github.com/mozilla/geckodriver/releases +""" + +BASE_URL = 'http://localhost:1313' +options = Options() +if os.path.exists('/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox'): + options.binary_location = '/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox' +options.add_argument('--headless') +DRIVER = webdriver.Firefox(options=options) +# To offset screen size based on window size +DRIVER.set_window_size(1500, 1085) + +SCREENSHOT_OPTIONS = [ + {'path': '/', 'filename': 'images/screenshot-{color_pref}-home.png'}, + {'path': '/posts/', 'filename': 'images/screenshot-{color_pref}-list.png'}, + {'path': '/posts/theme-documentation-basics/', 'filename': 'images/screenshot-{color_pref}-post.png'}, +] + +DARK_THEME = (32, 32, 32, 255) +LIGHT_THEME = (255, 255, 255, 255) + +def get_dominant_color(img_path): + pil_img = Image.open(img_path) + img = pil_img.copy() + img.convert("RGB") + img.resize((1, 1), resample=0) + dominant_color = img.getpixel((0, 0)) + return dominant_color + +def take_screenshots(driver, screenshot_options): + for opt in screenshot_options: + driver.get(BASE_URL + opt['path']) + color_a_filename = opt['filename'].format(color_pref='color-a') + color_b_filename = opt['filename'].format(color_pref='color-b') + + driver.save_screenshot(color_a_filename) + + # Change theme by clicking the theme toggle button + for elem in driver.find_elements_by_class_name('dark-theme-toggle'): + # Hidden hamburger menu for mobile raises Exception on clicking + if elem.is_displayed(): + elem.click() + + driver.save_screenshot(color_b_filename) + + for f in (color_a_filename, color_b_filename): + if get_dominant_color(f) == LIGHT_THEME: + os.rename(f, opt['filename'].format(color_pref='light')) + elif get_dominant_color(f) == DARK_THEME: + os.rename(f, opt['filename'].format(color_pref='dark')) + else: + raise Exception('Image does not match theme') + + print(f'Saved {opt["path"]}') + +def merge_home_images(): + light_home = Image.open('images/screenshot-light-home.png') + dark_home = Image.open('images/screenshot-dark-home.png') + if light_home.size != dark_home.size: + raise Exception('Image sizes should be same') + + width, height = light_home.size + + light_home_half = light_home.crop((0, 0, int(width/2), height)) + dark_home_half = dark_home.crop((int(width/2), 0, width, height)) + + merged_img = Image.new('RGB', (width, height)) + # Since we are merging horizontally, we will keep the offset of x-axis + x_offset = 0 + for img in (light_home_half, dark_home_half): + merged_img.paste(img, (x_offset, 0)) + x_offset += img.size[0] + + merged_img.save('images/screenshot.png') + merged_img.save('images/tn.png') + print('Created merged image for hugo showcase') + +take_screenshots(DRIVER, SCREENSHOT_OPTIONS) +merge_home_images() +DRIVER.quit()