#!/usr/bin/env python3 """ Generate images from HTML mockup files Uses Playwright to render HTML and take screenshots """ import os import sys from pathlib import Path try: from playwright.sync_api import sync_playwright PLAYWRIGHT_AVAILABLE = True except ImportError: PLAYWRIGHT_AVAILABLE = False try: from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service SELENIUM_AVAILABLE = True except ImportError: SELENIUM_AVAILABLE = False try: from html2image import Html2Image HTML2IMAGE_AVAILABLE = True except ImportError: HTML2IMAGE_AVAILABLE = False def generate_with_playwright(html_file, output_file, width=1920, height=1080): """Generate image using Playwright""" with sync_playwright() as p: browser = p.chromium.launch(headless=True) page = browser.new_page(viewport={'width': width, 'height': height}) page.goto(f'file://{html_file.absolute()}') # Wait for page to load page.wait_for_timeout(1000) page.screenshot(path=str(output_file), full_page=True) browser.close() print(f"✓ Generated {output_file.name} using Playwright") def generate_with_selenium(html_file, output_file, width=1920, height=1080): """Generate image using Selenium""" chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument(f'--window-size={width},{height}') driver = webdriver.Chrome(options=chrome_options) try: driver.get(f'file://{html_file.absolute()}') # Wait for page to load import time time.sleep(2) driver.save_screenshot(str(output_file)) print(f"✓ Generated {output_file.name} using Selenium") finally: driver.quit() def generate_with_html2image(html_file, output_file, width=1920, height=1080): """Generate image using html2image""" hti = Html2Image(size=(width, height)) hti.screenshot( html_file=str(html_file), save_as=output_file.name, size=(width, height) ) print(f"✓ Generated {output_file.name} using html2image") def generate_image(html_file, output_dir, width=1920, height=1080): """Generate image from HTML file using available method""" html_path = Path(html_file) output_path = output_dir / f"{html_path.stem}.png" if PLAYWRIGHT_AVAILABLE: try: generate_with_playwright(html_path, output_path, width, height) return True except Exception as e: print(f"Playwright failed: {e}, trying alternatives...") if SELENIUM_AVAILABLE: try: generate_with_selenium(html_path, output_path, width, height) return True except Exception as e: print(f"Selenium failed: {e}, trying alternatives...") if HTML2IMAGE_AVAILABLE: try: generate_with_html2image(html_path, output_path, width, height) return True except Exception as e: print(f"html2image failed: {e}") return False def main(): """Main function to generate images from all HTML files""" script_dir = Path(__file__).parent output_dir = script_dir / "images" output_dir.mkdir(exist_ok=True) html_files = list(script_dir.glob("*.html")) if not html_files: print("No HTML files found in mockups directory") return print(f"Found {len(html_files)} HTML file(s)") print(f"Output directory: {output_dir}") print() # Check available libraries if not any([PLAYWRIGHT_AVAILABLE, SELENIUM_AVAILABLE, HTML2IMAGE_AVAILABLE]): print("ERROR: No screenshot library available!") print("\nPlease install one of the following:") print(" pip install playwright && playwright install chromium") print(" pip install selenium") print(" pip install html2image") sys.exit(1) print("Available screenshot libraries:") if PLAYWRIGHT_AVAILABLE: print(" ✓ Playwright") if SELENIUM_AVAILABLE: print(" ✓ Selenium") if HTML2IMAGE_AVAILABLE: print(" ✓ html2image") print() # Generate images success_count = 0 for html_file in html_files: print(f"Generating image from {html_file.name}...") if generate_image(html_file, output_dir): success_count += 1 else: print(f"✗ Failed to generate image from {html_file.name}") print() print(f"Successfully generated {success_count}/{len(html_files)} images") print(f"Images saved to: {output_dir}") if __name__ == "__main__": main()