import os
import secrets 
from PIL import Image
from flask import current_app

def save_image(form_image):
    random_hex = secrets.token_hex(8) # type: ignore
    _, f_ext = os.path.splitext(form_image.filename)
    image_fn = random_hex + f_ext
    image_path = os.path.join(current_app.root_path, 'static/img', image_fn)
    
    output_size = (800, 800)
    i = Image.open(form_image)
    i.thumbnail(output_size)
    i.save(image_path)
    
    return image_fn