|
import os |
|
import re |
|
import shutil |
|
|
|
from PIL import Image, ImageDraw |
|
|
|
from config import MODEL_OUTPUT_IMAGE_NAMES |
|
|
|
ROOT_DIR = "benchmark_images_generations" |
|
OUTPUT_DIR = "data/" |
|
|
|
def create_image_with_bbox_border( |
|
bg_image_path: str, |
|
mask_image_path: str, |
|
output_image_path: str, |
|
border_color: tuple = (245, 40, 145, 205), |
|
border_width: int = 5, |
|
) -> None: |
|
""" |
|
Opens a background image and a mask image (defining a rectangular bounding box), |
|
ensures they have the same size, then creates and saves an image showing the |
|
background with a semi-transparent border of the bounding box to the specified |
|
output path. No console output is produced by this function, and no image is displayed. |
|
|
|
Args: |
|
bg_image_path (str): Path to the background image. |
|
mask_image_path (str): Path to the mask image (expected to be black |
|
with a white rectangle defining the box). |
|
output_image_path (str): Path where the resulting image will be saved. |
|
border_color (tuple): RGBA tuple for the border color. |
|
The alpha component (0-255) controls transparency. |
|
border_width (int): Width of the bounding box border in pixels. |
|
|
|
Raises: |
|
FileNotFoundError: If either the background or mask image file is not found. |
|
ValueError: If no bounding box (non-black area) is found in the mask image. |
|
Exception: For other PIL-related errors during image processing (e.g., |
|
unsupported file format, issues during drawing, or saving errors). |
|
This includes TypeError if the installed Pillow version does not |
|
support the 'width' parameter for `ImageDraw.rectangle`. |
|
""" |
|
|
|
|
|
bg_image = Image.open(bg_image_path) |
|
mask_image = Image.open(mask_image_path) |
|
|
|
|
|
if bg_image.size != mask_image.size: |
|
|
|
mask_image = mask_image.resize(bg_image.size, Image.NEAREST) |
|
|
|
|
|
if bg_image.mode != 'RGBA': |
|
bg_image = bg_image.convert('RGBA') |
|
|
|
|
|
|
|
|
|
mask_gray = mask_image.convert('L') |
|
|
|
|
|
|
|
bbox = mask_gray.getbbox() |
|
|
|
if bbox is None: |
|
raise ValueError("No bounding box found in the mask image (it might be entirely black or invalid).") |
|
|
|
|
|
draw = ImageDraw.Draw(bg_image) |
|
|
|
|
|
|
|
|
|
|
|
draw.rectangle(bbox, outline=border_color, width=border_width) |
|
|
|
|
|
|
|
bg_image.save(output_image_path) |
|
|
|
|
|
|
|
|
|
def main(): |
|
for domain in os.listdir(ROOT_DIR): |
|
domain_dir = os.path.join(ROOT_DIR, domain) |
|
for i, sample_dir in enumerate(os.listdir(domain_dir)): |
|
if sample_dir == ".DS_Store": |
|
continue |
|
sample_dir_path = os.path.join(domain_dir, sample_dir) |
|
prompt = sample_dir[4:].strip() |
|
|
|
output_sample_dir = os.path.join(OUTPUT_DIR, domain, f"sample_{i}") |
|
os.makedirs(output_sample_dir, exist_ok=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
input_bg = None |
|
for file in os.listdir(sample_dir_path): |
|
if re.match(r"bg\d+\.(jpg|png)", file): |
|
input_bg = file |
|
break |
|
if input_bg: |
|
input_bg_path = os.path.join(sample_dir_path, input_bg) |
|
shutil.copy(input_bg_path, os.path.join(output_sample_dir, "input_bg.jpg")) |
|
else: |
|
print(f"Warning: No input background found in {sample_dir_path}. Skipping sample {i}...") |
|
continue |
|
|
|
|
|
|
|
|
|
|
|
input_fg = None |
|
for file in os.listdir(sample_dir_path): |
|
if re.match(r"fg(_\w+)?\.(jpg|png)", file) or re.match(r"fg\d+(_\w+)?\.(jpg|png)", file): |
|
if "mask" not in file: |
|
input_fg = file |
|
break |
|
if input_fg: |
|
input_fg_path = os.path.join(sample_dir_path, input_fg) |
|
shutil.copy(input_fg_path, os.path.join(output_sample_dir, "input_fg.jpg")) |
|
else: |
|
print(f"Warning: No input foreground found in {sample_dir_path}. Skipping sample {i}...") |
|
continue |
|
|
|
|
|
input_bb = None |
|
for file in os.listdir(sample_dir_path): |
|
if file == "mask_bg_fg.jpg" or file == "mask_bg_fg.png": |
|
input_bb = file |
|
break |
|
if input_bb and input_bg: |
|
|
|
create_image_with_bbox_border( |
|
bg_image_path=os.path.join(output_sample_dir, "input_bg.jpg"), |
|
mask_image_path=os.path.join(sample_dir_path, input_bb), |
|
output_image_path=os.path.join(output_sample_dir, "input_bg_bb.png"), |
|
) |
|
|
|
|
|
if not all( |
|
[ |
|
os.path.exists(os.path.join(sample_dir_path, "cp_bg_fg.jpg")), |
|
os.path.exists(os.path.join(sample_dir_path, "kvedit.jpg")), |
|
os.path.exists(os.path.join(sample_dir_path, "tf-icon.png")), |
|
] |
|
) or not any( |
|
[ |
|
|
|
|
|
os.path.exists(os.path.join(sample_dir_path, "alphanoise0.05_timesteps50_QTrue_KTrue_VFalse_taua0.4_taub0.8_guidance3.0_all-layers.png")), |
|
] |
|
): |
|
print(f"Warning: Not all output images found in {sample_dir_path}. Skipping sample {i}...") |
|
|
|
shutil.rmtree(output_sample_dir) |
|
continue |
|
|
|
for model_name, image_name in MODEL_OUTPUT_IMAGE_NAMES.items(): |
|
|
|
image_path = os.path.join(sample_dir_path, image_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
target_path = os.path.join(output_sample_dir, image_name) |
|
if os.path.exists(image_path): |
|
shutil.copy(image_path, target_path) |
|
else: |
|
print(f"Warning: {image_name} not found in {sample_dir_path}. Skipping...") |
|
|
|
|
|
|
|
prompt_file_path = os.path.join(output_sample_dir, "prompt.txt") |
|
with open(prompt_file_path, "w") as prompt_file: |
|
prompt_file.write(prompt) |
|
|
|
if __name__ == "__main__": |
|
main() |