jasonfan's picture
2026-03-19: ICL code
64bce2a verified
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
# Input and output paths
slerp_dir = "/hpc/group/chenglab/zl310/spring2025_projects/InterpStableDiffusion/results/interp_res_slerp"
output_base_dir = "/hpc/group/chenglab/jf381/nips2025_paperfigure"
# Create output directories
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"}
}
# Create directories for each format
for format_name in formats:
format_dir = os.path.join(output_base_dir, format_name)
os.makedirs(format_dir, exist_ok=True)
# Get the list of prompt folders (sorted)
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)
# Take the first 20 prompt folders
prompt_folders = prompt_folders[:20]
# Temporary directory for processed images
temp_dir = os.path.join(output_base_dir, "temp")
os.makedirs(temp_dir, exist_ok=True)
# Function to process image
def process_image(img_path, format_spec):
img = Image.open(img_path)
# Convert to the correct mode
if format_spec["mode"] == "RGB":
processed_img = img.convert('RGB')
else: # Grayscale
processed_img = img.convert('L')
# Resize if needed
if format_spec["size"]:
processed_img = processed_img.resize(format_spec["size"], Image.LANCZOS)
return processed_img
# Create PowerPoint presentations for each format
for format_name, format_spec in formats.items():
print(f"\nProcessing {format_name}...")
# Create presentations for different grid layouts
prs_2x10 = Presentation()
prs_4x5 = Presentation()
# Set slide size to a standard 16:9 format
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)
# Process each prompt folder
for prompt_folder in prompt_folders:
print(f" Processing {prompt_folder}...")
# Full path to the prompt folder
prompt_path = os.path.join(slerp_dir, prompt_folder)
# Get all image files in the 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]))
# Select 20 evenly spaced images for both layouts
total_images = len(image_files)
indices = np.linspace(0, total_images - 1, 20).astype(int)
selected_images = [image_files[i] for i in indices]
# Process all selected images
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)
# Save to temp directory
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)
# Create 2x10 grid slide
slide_2x10 = prs_2x10.slides.add_slide(prs_2x10.slide_layouts[5]) # Title and Content layout
slide_2x10.shapes.title.text = f"{prompt_folder} (2×10 Grid)"
# Make title smaller and move it up
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)
# Calculate grid dimensions for 2x10
grid_width = prs_2x10.slide_width - Inches(0.5) # Leave minimal margins
grid_height = prs_2x10.slide_height - Inches(0.6) # Account for title
cell_width_2x10 = grid_width / 10
cell_height_2x10 = grid_height / 2
# Add images to 2x10 grid with minimal spacing
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) # Start below title
# Calculate image dimensions
img = Image.open(img_path)
img_aspect = img.width / img.height
# Tight fit with minimal spacing
width = cell_width_2x10 * 0.98
height = width / img_aspect
# If height exceeds cell, constrain by height
if height > cell_height_2x10 * 0.98:
height = cell_height_2x10 * 0.98
width = height * img_aspect
# Add image to slide
slide_2x10.shapes.add_picture(img_path, left, top, width=width, height=height)
# Create 4x5 grid slide
slide_4x5 = prs_4x5.slides.add_slide(prs_4x5.slide_layouts[5]) # Title and Content layout
slide_4x5.shapes.title.text = f"{prompt_folder} (4×5 Grid)"
# Make title smaller and move it up
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)
# Calculate grid dimensions for 4x5
grid_width = prs_4x5.slide_width - Inches(0.5) # Leave minimal margins
grid_height = prs_4x5.slide_height - Inches(0.6) # Account for title
cell_width_4x5 = grid_width / 5
cell_height_4x5 = grid_height / 4
# Add images to 4x5 grid with minimal spacing
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) # Start below title
# Calculate image dimensions
img = Image.open(img_path)
img_aspect = img.width / img.height
# Tight fit with minimal spacing
width = cell_width_4x5 * 0.98
height = width / img_aspect
# If height exceeds cell, constrain by height
if height > cell_height_4x5 * 0.98:
height = cell_height_4x5 * 0.98
width = height * img_aspect
# Add image to slide
slide_4x5.shapes.add_picture(img_path, left, top, width=width, height=height)
# Save the PowerPoint presentations
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}")
# Clean up temporary directory
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.")