File size: 5,465 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 | 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演示文稿。") |