import sys
from PIL import Image

def remove_background(input_path, output_path, threshold=240):
    print(f"Loading image from {input_path}...")
    img = Image.open(input_path)
    img = img.convert("RGBA")
    datas = img.getdata()

    new_data = []
    for item in datas:
        # item is a tuple of (R, G, B, A)
        r, g, b, a = item
        # If the pixel is close to white (R, G, B all greater than threshold)
        if r >= threshold and g >= threshold and b >= threshold:
            # Make it fully transparent
            new_data.append((r, g, b, 0))
        else:
            new_data.append(item)

    img.putdata(new_data)
    print(f"Saving transparent image to {output_path}...")
    img.save(output_path, "PNG")
    print("Done!")

if __name__ == "__main__":
    remove_background("assets/images/loopo.png", "assets/images/logo_official.png", threshold=240)
