| import os |
| import numpy as np |
| from PIL import Image |
| from pptx import Presentation |
| from pptx.util import Inches, Pt |
| from pptx.enum.shapes import MSO_SHAPE |
| from pptx.dml.color import RGBColor |
|
|
| |
| slerp_dir = "/hpc/group/chenglab/zl310/spring2025_projects/InterpStableDiffusion/results/interp_res_slerp" |
| output_base_dir = "/hpc/group/chenglab/jf381/nips2025_paperfigure" |
|
|
| |
| formats = { |
| "full_size_rgb": {"size": None, "mode": "RGB"}, |
| "32x32_rgb": {"size": (32, 32), "mode": "RGB"}, |
| "full_size_gray": {"size": None, "mode": "L"}, |
| "32x32_gray": {"size": (32, 32), "mode": "L"} |
| } |
|
|
| |
| for format_name in formats: |
| format_dir = os.path.join(output_base_dir, format_name) |
| os.makedirs(format_dir, exist_ok=True) |
|
|
| |
| prompt_folders = [f for f in os.listdir(slerp_dir) if f.startswith('prompt_') and os.path.isdir(os.path.join(slerp_dir, f))] |
| prompt_folders.sort(key=lambda x: int(x.split('_')[-1]) if x.split('_')[-1].isdigit() else 0) |
|
|
| |
| prompt_folders = prompt_folders[:20] |
|
|
| |
| temp_dir = os.path.join(output_base_dir, "temp") |
| os.makedirs(temp_dir, exist_ok=True) |
|
|
| |
| def process_image(img_path, format_spec): |
| img = Image.open(img_path) |
| |
| |
| if format_spec["mode"] == "RGB": |
| processed_img = img.convert('RGB') |
| else: |
| processed_img = img.convert('L') |
| |
| |
| if format_spec["size"]: |
| processed_img = processed_img.resize(format_spec["size"], Image.LANCZOS) |
| |
| return processed_img |
|
|
| |
| for format_name, format_spec in formats.items(): |
| print(f"\nProcessing {format_name}...") |
| |
| |
| prs_2x10 = Presentation() |
| prs_4x5 = Presentation() |
| |
| |
| prs_2x10.slide_width = Inches(13.33) |
| prs_2x10.slide_height = Inches(7.5) |
| prs_4x5.slide_width = Inches(13.33) |
| prs_4x5.slide_height = Inches(7.5) |
| |
| |
| for prompt_folder in prompt_folders: |
| print(f" Processing {prompt_folder}...") |
| |
| |
| prompt_path = os.path.join(slerp_dir, prompt_folder) |
| |
| |
| image_files = [f for f in os.listdir(prompt_path) if f.startswith('noise_interp_img_') and f.endswith('.png')] |
| image_files.sort(key=lambda x: int(x.split('_')[-1].split('.')[0])) |
| |
| |
| total_images = len(image_files) |
| indices = np.linspace(0, total_images - 1, 20).astype(int) |
| selected_images = [image_files[i] for i in indices] |
| |
| |
| processed_image_paths = [] |
| for idx, img_file in enumerate(selected_images): |
| img_path = os.path.join(prompt_path, img_file) |
| processed_img = process_image(img_path, format_spec) |
| |
| |
| temp_path = os.path.join(temp_dir, f"{format_name}_{prompt_folder}_{idx}.png") |
| processed_img.save(temp_path) |
| processed_image_paths.append(temp_path) |
| |
| |
| slide_2x10 = prs_2x10.slides.add_slide(prs_2x10.slide_layouts[5]) |
| slide_2x10.shapes.title.text = f"{prompt_folder} (2×10 Grid)" |
| |
| |
| title_shape = slide_2x10.shapes.title |
| title_shape.top = Inches(0.1) |
| title_shape.height = Inches(0.4) |
| title_shape.text_frame.paragraphs[0].font.size = Pt(18) |
| |
| |
| grid_width = prs_2x10.slide_width - Inches(0.5) |
| grid_height = prs_2x10.slide_height - Inches(0.6) |
| |
| cell_width_2x10 = grid_width / 10 |
| cell_height_2x10 = grid_height / 2 |
| |
| |
| for idx, img_path in enumerate(processed_image_paths): |
| row = idx // 10 |
| col = idx % 10 |
| |
| left = Inches(0.25) + (cell_width_2x10 * col) |
| top = Inches(0.55) + (cell_height_2x10 * row) |
| |
| |
| img = Image.open(img_path) |
| img_aspect = img.width / img.height |
| |
| |
| width = cell_width_2x10 * 0.98 |
| height = width / img_aspect |
| |
| |
| if height > cell_height_2x10 * 0.98: |
| height = cell_height_2x10 * 0.98 |
| width = height * img_aspect |
| |
| |
| slide_2x10.shapes.add_picture(img_path, left, top, width=width, height=height) |
| |
| |
| slide_4x5 = prs_4x5.slides.add_slide(prs_4x5.slide_layouts[5]) |
| slide_4x5.shapes.title.text = f"{prompt_folder} (4×5 Grid)" |
| |
| |
| title_shape = slide_4x5.shapes.title |
| title_shape.top = Inches(0.1) |
| title_shape.height = Inches(0.4) |
| title_shape.text_frame.paragraphs[0].font.size = Pt(18) |
| |
| |
| grid_width = prs_4x5.slide_width - Inches(0.5) |
| grid_height = prs_4x5.slide_height - Inches(0.6) |
| |
| cell_width_4x5 = grid_width / 5 |
| cell_height_4x5 = grid_height / 4 |
| |
| |
| for idx, img_path in enumerate(processed_image_paths): |
| row = idx // 5 |
| col = idx % 5 |
| |
| left = Inches(0.25) + (cell_width_4x5 * col) |
| top = Inches(0.55) + (cell_height_4x5 * row) |
| |
| |
| img = Image.open(img_path) |
| img_aspect = img.width / img.height |
| |
| |
| width = cell_width_4x5 * 0.98 |
| height = width / img_aspect |
| |
| |
| if height > cell_height_4x5 * 0.98: |
| height = cell_height_4x5 * 0.98 |
| width = height * img_aspect |
| |
| |
| slide_4x5.shapes.add_picture(img_path, left, top, width=width, height=height) |
| |
| |
| ppt_2x10_path = os.path.join(output_base_dir, format_name, f"paper_grid_2x10.pptx") |
| prs_2x10.save(ppt_2x10_path) |
| print(f" Saved 2x10 grid presentation for {format_name}") |
| |
| ppt_4x5_path = os.path.join(output_base_dir, format_name, f"paper_grid_4x5.pptx") |
| prs_4x5.save(ppt_4x5_path) |
| print(f" Saved 4x5 grid presentation for {format_name}") |
|
|
| |
| for file in os.listdir(temp_dir): |
| os.remove(os.path.join(temp_dir, file)) |
| os.rmdir(temp_dir) |
|
|
| print("\nAll done! PowerPoint presentations with tight grid layouts created for each format.") |