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
# 输入和输出路径
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)
# 取前20个提示文件夹
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
# 为每种格式创建PowerPoint演示文稿
for format_name, format_spec in formats.items():
print(f"\n处理 {format_name}...")
# 创建10x10网格布局的演示文稿
prs_10x10 = Presentation()
# 设置幻灯片大小为标准16:9格式
prs_10x10.slide_width = Inches(13.33)
prs_10x10.slide_height = Inches(7.5)
# 处理每个提示文件夹
for prompt_folder in prompt_folders:
print(f" 处理 {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]))
# 选择100张均匀间隔的图像用于10x10网格
total_images = len(image_files)
indices = np.linspace(0, total_images - 1, 100).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)
# 创建10x10网格幻灯片
slide_10x10 = prs_10x10.slides.add_slide(prs_10x10.slide_layouts[5]) # 标题和内容布局
slide_10x10.shapes.title.text = f"{prompt_folder} (10×10 紧凑网格)"
# 使标题更小并将其上移
title_shape = slide_10x10.shapes.title
title_shape.top = Inches(0.1)
title_shape.height = Inches(0.4)
title_shape.text_frame.paragraphs[0].font.size = Pt(18)
# 计算10x10网格的尺寸
grid_width = prs_10x10.slide_width - Inches(0.5) # 留出最小边距
grid_height = prs_10x10.slide_height - Inches(0.6) # 考虑标题
cell_width_10x10 = grid_width / 10
cell_height_10x10 = grid_height / 10
# 添加图像到10x10网格,无间距
for idx, img_path in enumerate(processed_image_paths):
row = idx // 10
col = idx % 10
left = Inches(0.25) + (cell_width_10x10 * col)
top = Inches(0.55) + (cell_height_10x10 * row) # 从标题下方开始
# 计算图像尺寸
img = Image.open(img_path)
img_aspect = img.width / img.height
# 完全紧凑布局,无间距
width = cell_width_10x10
height = width / img_aspect
# 如果高度超过单元格,则按高度约束
if height > cell_height_10x10:
height = cell_height_10x10
width = height * img_aspect
# 将图像添加到幻灯片
slide_10x10.shapes.add_picture(img_path, left, top, width=width, height=height)
# 保存PowerPoint演示文稿
ppt_10x10_path = os.path.join(output_base_dir, format_name, f"paper_grid_10x10_compact.pptx")
prs_10x10.save(ppt_10x10_path)
print(f" 已保存{format_name}的10x10紧凑网格演示文稿")
# 清理临时目录
for file in os.listdir(temp_dir):
os.remove(os.path.join(temp_dir, file))
os.rmdir(temp_dir)
print("\n完成!为每种格式创建了紧凑型10x10网格布局的PowerPoint演示文稿。")