|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
resize_size = (477, 188) |
|
|
line_width = 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 |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
cropped = img.crop((left, top, right, bottom)) |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
for i in tqdm(range(0, len(files), 6), desc="拼接图片"): |
|
|
|
|
|
merged_image = np.zeros((target_merge_size[1], target_merge_size[0], 3), dtype=np.uint8) |
|
|
|
|
|
|
|
|
valid_imgs = [] |
|
|
for j in range(6): |
|
|
if i + j >= len(files): |
|
|
|
|
|
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: |
|
|
|
|
|
img = Image.open(img_path).convert("RGB") |
|
|
img_w, img_h = img.size |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
cropped_img = center_crop_to_size(img, crop_size) |
|
|
|
|
|
|
|
|
resized_img = cropped_img.resize(resize_size, resample=Image.LANCZOS) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
assert len(valid_imgs) == 6, "有效图片数量必须为6张" |
|
|
|
|
|
total_col = 2 * resize_size[0] + 1 * line_width |
|
|
total_row = 3 * resize_size[1] + 2 * line_width |
|
|
|
|
|
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 |
|
|
col = idx % 2 |
|
|
|
|
|
|
|
|
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, :] |
|
|
|
|
|
|
|
|
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}") |