""" Generate image files for each comic page at 800x1080 resolution Simple version that creates HTML canvases instead of actual image files """ import os import json from typing import List, Dict class PageImageGenerator: """Generate page images as HTML canvases that can be saved""" def __init__(self, output_dir: str = "output/page_images"): self.output_dir = output_dir self.page_size = (800, 1080) # Width x Height def generate_page_images(self, pages_data: List[Dict], frames_dir: str) -> List[str]: """Generate HTML pages that render as images""" os.makedirs(self.output_dir, exist_ok=True) generated_files = [] # Create individual HTML files for each page for i, page in enumerate(pages_data): filename = f"page_{i+1:03d}.html" filepath = os.path.join(self.output_dir, filename) self._create_page_html(page, frames_dir, i + 1, filepath) generated_files.append(filepath) print(f"📄 Generated page {i+1}/{len(pages_data)}: {filename}") # Create main gallery self._create_gallery_html(len(pages_data)) return generated_files def _create_page_html(self, page: Dict, frames_dir: str, page_num: int, output_path: str): """Create HTML that renders a comic page at 800x1080""" panels_html = "" panels = page.get('panels', []) for idx, panel in enumerate(panels[:4]): if panel.get('image'): img_path = f"../../frames/final/{panel['image']}" bubble_html = "" if panel.get('speech_bubble'): bubble = panel['speech_bubble'] bubble_html = f"""
{bubble.get('text', '')}
""" panels_html += f"""
Panel {idx+1} {bubble_html}
""" html_content = f""" Comic Page {page_num}
{panels_html}
Page {page_num}
""" with open(output_path, 'w', encoding='utf-8') as f: f.write(html_content) def _create_gallery_html(self, num_pages: int): """Create gallery index HTML""" page_links = "" for i in range(num_pages): page_num = i + 1 filename = f"page_{page_num:03d}.html" page_links += f"""
{page_num}
Page {page_num}
🔍 View 📥 Download HTML
""" html_content = f""" Comic Page Images Gallery

📚 Comic Page Images

All pages rendered at 800x1080 resolution

{num_pages} pages generated

💡 How to save as images:
  1. Click on any page to view it
  2. Click "Download as Image" button
  3. In print dialog: Select "Save as PDF"
  4. Or take a screenshot (better quality)
📂 Open All Pages
""" index_path = os.path.join(self.output_dir, 'index.html') with open(index_path, 'w', encoding='utf-8') as f: f.write(html_content) print(f"📋 Page gallery created: {index_path}") def generate_page_images_from_json(json_path: str, frames_dir: str, output_dir: str = None): """Standalone function to generate page images from pages.json""" if not os.path.exists(json_path): print(f"❌ Pages JSON not found: {json_path}") return [] # Load pages data with open(json_path, 'r') as f: pages_data = json.load(f) # Create generator generator = PageImageGenerator(output_dir or "output/page_images") # Generate images return generator.generate_page_images(pages_data, frames_dir)