File size: 8,200 Bytes
feb33a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# import os
# render2real_path = "/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/spotlight_sketch/epoch0"
# sketch_enhance_body_path = "/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/spotlight_sketch/GT"
# render2real_files = set(os.listdir(render2real_path))
# sketch_enhance_body_files = set(os.listdir(sketch_enhance_body_path))
# for file in render2real_files:
#     if file not in sketch_enhance_body_files:
#         print(f"Removing {file} from render2real_path")
#         os.remove(os.path.join(render2real_path, file))
# for file in sketch_enhance_body_files:
#     if file not in render2real_files:
#         print(f"Removing {file} from sketch_enhance_body_path")
#         os.remove(os.path.join(sketch_enhance_body_path, file))

# import os
# input_path = "/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/mbti/Realistic"
# for file in os.listdir(input_path):
#     # 把文件名称最后的_Realistic去掉
#     new_name = file.replace("_Realistic", "")
#     os.rename(os.path.join(input_path, file), os.path.join(input_path, new_name))

# import os
# for file in os.listdir("dataset/spotlight_sketch_cat/GT"):
#     with open("dataset/spotlight_sketch_cat/pairs_t2t.txt", "a") as f:  
#         # 目标图 原图          
#         f.write(f"GT/{file}\tepoch0/{file}\n")


# import os
# import json
# from tqdm import tqdm
# input_txt = "/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/spotlight_sketch_cat/spotlight_nano_comprehension_1203.txt"
# with open(input_txt, "r") as f:
#     lines = f.readlines()
#     for i in tqdm(range(len(lines))):
#         data = json.loads(lines[i])   
#         fig_id = f"{data['Image_Name']}.png"
#         del data["Image_Name"]
#         input_dir = "dataset/spotlight_sketch_cat/epoch0"
#         GT_dir = "dataset/spotlight_sketch_cat/GT"
#         for file in os.listdir(input_dir): 
#             if fig_id in file:
#                 with open("dataset/spotlight_sketch_cat/pairs_i2i.txt", "a") as f:
#                     # 目标 原图 prompt
#                     f.write(f"{GT_dir}/{file}\t{input_dir}/{file}\t{data}\n")

# 把文件夹中的图片每六张拼成一个3行两列的大图,保存到另一个文件夹中,原图拼接不要截图
import os
from PIL import Image
from tqdm import tqdm
import numpy as np
base_dirs = ["/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/the roses","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/nouvelle","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/legs","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/frankenstein"]
# 核心配置参数(按需求定义)
crop_size = (1920, 800)    # 目标CenterCrop尺寸 (宽, 高)
resize_size = (477, 188)   # 下采样后的单张尺寸 (宽, 高)
line_width = 6             # 黑线宽度(6像素)
target_merge_size = (960, 576)  # 最终拼接目标尺寸 (宽, 高)


def center_crop_to_size(img, target_size):
    """
    对图片进行CenterCrop到指定尺寸,不足部分用黑色像素填充
    :param img: PIL Image对象
    :param target_size: (target_w, target_h) 目标裁剪尺寸
    :return: crop+补黑后的PIL Image
    """
    target_w, target_h = target_size
    img_w, img_h = img.size
    
    # Step1: 计算CenterCrop的区域(中心对齐)
    # 水平方向裁剪
    if img_w >= target_w:
        left = (img_w - target_w) // 2
        right = left + target_w
    else:
        left = 0
        right = img_w
    # 垂直方向裁剪
    if img_h >= target_h:
        top = (img_h - target_h) // 2
        bottom = top + target_h
    else:
        top = 0
        bottom = img_h
    
    # Step2: 执行CenterCrop
    cropped = img.crop((left, top, right, bottom))
    
    # Step3: 不足目标尺寸的部分用黑色填充
    if cropped.size != (target_w, target_h):
        new_img = Image.new("RGB", (target_w, target_h), (0, 0, 0))  # 黑色背景
        new_img.paste(cropped, ((target_w - cropped.width) // 2, (target_h - cropped.height) // 2))
        cropped = new_img
    
    return cropped
for k in range(len(base_dirs)):
    save_path = f"{base_dirs[k]}_dedup_cat" 
    os.makedirs(save_path, exist_ok=True)
    input_path = f"{base_dirs[k]}_dedup"
# 获取并排序文件列表
    files = [f for f in os.listdir(input_path) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
    files.sort()

    # 遍历文件,每6张拼接一次
    for i in tqdm(range(0, len(files), 6), desc="拼接图片"):
        # 初始化最终拼接画布(目标尺寸960×576,黑色背景)
        merged_image = np.zeros((target_merge_size[1], target_merge_size[0], 3), dtype=np.uint8)
        
        # 逐个处理6张图片
        valid_imgs = []  # 存储处理后的有效图片
        for j in range(6):
            if i + j >= len(files):
                # 不足6张时,break
                img_np = np.zeros((resize_size[1], resize_size[0], 3), dtype=np.uint8)
                valid_imgs.append(img_np)
                continue
            
            img_path = os.path.join(input_path, files[i + j])
            try:
                # 读取图片(确保RGB格式)
                img = Image.open(img_path).convert("RGB")
                img_w, img_h = img.size
                
                # 过滤条件:原始宽度不足1800则跳过并打印
                if img_w < 1800:
                    print(f"跳过文件 {files[i+j]}: 原始宽度 {img_w} < 1800")
                    # 用黑色图片填充该位置
                    img_np = np.zeros((resize_size[1], resize_size[0], 3), dtype=np.uint8)
                    valid_imgs.append(img_np)
                    continue
                
                # Step1: CenterCrop到1920×800,不足补黑
                cropped_img = center_crop_to_size(img, crop_size)
                
                # Step2: 下采样到477×188(LANCZOS插值,保持画质)
                resized_img = cropped_img.resize(resize_size, resample=Image.LANCZOS)
                
                # 转为numpy数组
                img_np = np.array(resized_img)
                valid_imgs.append(img_np)
            
            except Exception as e:
                print(f"处理文件 {files[i+j]} 出错: {str(e)}")
                # 出错时用黑色图片填充
                img_np = np.zeros((resize_size[1], resize_size[0], 3), dtype=np.uint8)
                valid_imgs.append(img_np)
        
        # Step3: 计算每张图在拼接画布中的位置(3行2列 + 6像素黑线)
        # 验证拼接尺寸兼容性(防止配置错误)
        assert len(valid_imgs) == 6, "有效图片数量必须为6张"
        # 计算图片+黑线的总占位,确保适配960×576
        total_col = 2 * resize_size[0] + 1 * line_width  # 2列+1条竖线
        total_row = 3 * resize_size[1] + 2 * line_width  # 3行+2条横线
        # 计算画布中的偏移(居中放置,保证最终尺寸960×576)
        offset_x = (target_merge_size[0] - total_col) // 2
        offset_y = (target_merge_size[1] - total_row) // 2
        
        # 逐个放置图片到拼接画布
        for idx, img_np in enumerate(valid_imgs):
            row = idx // 2  # 0/1/2行
            col = idx % 2   # 0/1列
            
            # 计算当前图片的起始位置(含黑线+整体偏移)
            x_start = offset_x + col * (resize_size[0] + line_width)
            y_start = offset_y + row * (resize_size[1] + line_width)
            x_end = x_start + resize_size[0]
            y_end = y_start + resize_size[1]
            
            # 确保不超出画布边界
            x_end = min(x_end, target_merge_size[0])
            y_end = min(y_end, target_merge_size[1])
            x_start = max(x_start, 0)
            y_start = max(y_start, 0)
            
            # 放置图片到画布
            merged_image[y_start:y_end, x_start:x_end, :] = img_np[:y_end-y_start, :x_end-x_start, :]
        
        # Step4: 保存最终拼接图片
        save_name = f'merged_{i//6}.png'
        save_full_path = os.path.join(save_path, save_name)
        Image.fromarray(merged_image).save(save_full_path)

    print(f"所有图片处理完成!结果保存至: {save_path}")