File size: 7,494 Bytes
64bce2a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 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.") |