import os
from PIL import Image

dir_path = 'public/sitio'
input_filename = 'logo-regular.png'
output_filename = 'logo-square-250.png'

input_path = os.path.join(dir_path, input_filename)
output_path = os.path.join(dir_path, output_filename)

if not os.path.exists(input_path):
    print(f"Error: {input_path} no encontrado.")
    exit(1)

# Cargar la imagen original
img = Image.open(input_path)

# Crear un lienzo cuadrado transparente de 250 x 250 px
canvas_size = (250, 250)
new_img = Image.new("RGBA", canvas_size, (255, 255, 255, 0)) # Transparente

# Limitar ancho a 210 px para evitar recortes en círculos de avatares
max_width = 210
ratio = max_width / img.width
new_w = int(img.width * ratio)
new_h = int(img.height * ratio)

# Redimensionar usando filtro de alta calidad
resized_img = img.resize((new_w, new_h), Image.Resampling.LANCZOS)

# Centrar la imagen en el lienzo
offset_x = (canvas_size[0] - new_w) // 2
offset_y = (canvas_size[1] - new_h) // 2

# Pegar la imagen redimensionada sobre el lienzo transparente
new_img.paste(resized_img, (offset_x, offset_y), resized_img)

# Guardar el nuevo logo cuadrado
new_img.save(output_path)
print(f"¡Logo cuadrado creado exitosamente en: {output_path}!")
print(f"Dimensiones: {new_img.size} px (Imagen interna redimensionada a {new_w}x{new_h} px)")
