#Auto image resizer - GPT
#DEPENDENCY - Python installed and "pip install pillow"
#Works on Windows
#You may need to run the script from cmd "python image_resizer.py" instead of double clicking on the script

from PIL import Image
import os

def resize_image(image_path, output_folder, size, output_format):
    img = Image.open(image_path)
    img = img.resize((size, size), Image.LANCZOS)
    filename = os.path.basename(image_path).split('.')[0] # Gets the id from the filename
    new_filename = os.path.join(output_folder, f"{filename}.{output_format}")
    img.save(new_filename, output_format.upper())

# INPUT Your master image file here
master_image_file = '1091.webp' #image in same base directory as the script with name coin_id. Works with .png, .webp, possible more
#master_image_file = '../Website images masters small/Logos/Coins/1094.png' #this was causing errors

# INPUT your base directory here
base_directory = "C:/Users/Steven/Documents/gitlab_projects/Website-Images"

# Get the id from the master image file name
id = master_image_file.split('/')[-1].split('.')[0]

# Resize and save the images to the respective folders
resize_image(master_image_file, base_directory + "/coins/", 256, 'png')
resize_image(master_image_file, base_directory + "/coins-webp/", 256, 'webp')
resize_image(master_image_file, base_directory + "/coins-small/", 32, 'png')
resize_image(master_image_file, base_directory + "/coins-tiny/", 20, 'png')
