0% found this document useful (0 votes)
3 views1 page

Black Square Images

processes images into a pix2pix dataset

Uploaded by

Papa Boudje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Black Square Images

processes images into a pix2pix dataset

Uploaded by

Papa Boudje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# filename: add_black_square.

py
import os
from PIL import Image, ImageDraw

def add_black_square_to_images(input_folder, output_folder, square_size=(100,


100)):
# Create the output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)

# Get a list of all files in the input folder


for filename in os.listdir(input_folder):
# Construct full file path
file_path = os.path.join(input_folder, filename)

# Only process image files


if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif',
'.tiff')):
with Image.open(file_path) as img:
# Calculate the position for the square
img_width, img_height = img.size
square_width, square_height = square_size
top_left_x = (img_width - square_width) // 2
top_left_y = (img_height - square_height) // 2
bottom_right_x = top_left_x + square_width
bottom_right_y = top_left_y + square_height

# Draw the black square on the image


draw = ImageDraw.Draw(img)
draw.rectangle([top_left_x, top_left_y, bottom_right_x,
bottom_right_y], fill="black")

# Save the modified image in the output folder


output_path = os.path.join(output_folder, filename)
img.save(output_path)

print(f"Processed and saved: {output_path}")

if __name__ == "__main__":
input_folder = "images/celeba/female"
output_folder = "images/celeba/female_black_square"
square_size=(250,250)
add_black_square_to_images(input_folder, output_folder,square_size=square_size)

You might also like