import os
from PIL import Image, ImageDraw, ImageFont
def resize_images(input_folder, output_folder, size=(384, 384), extra_space=50):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
try:
img = Image.open(input_path)
img = img.resize(size, Image.NEAREST)
new_size = (size[0], size[1] + extra_space)
new_img = Image.new("RGB", new_size, (255, 255, 255))
new_img.paste(img, (0, 0))
draw = ImageDraw.Draw(new_img)
try:
font = ImageFont.truetype("DejaVuSans-Bold.ttf", 22)
except IOError:
print("oops")
font = ImageFont.load_default()
text = filename[:filename.rfind(".")] if "." in filename else filename
text_position = (196, 407)
text_color = (0, 0, 0)
draw.text(text_position, text, fill=text_color, font=font, anchor="mm")
new_img.save(output_path)
print(f"Resized, added text, and saved: {output_path}")
except Exception as e:
print(f"Error processing {filename}: {e}")
if __name__ == "__main__":
input_folder = '/path/there'
output_folder = '/path/here'
resize_images(input_folder, output_folder)