from PIL import Image, ImageDraw

def make_transparent_edges(input_path, output_path):
    img = Image.open(input_path).convert("RGBA")
    
    # We will flood fill starting from the top-left corner (0,0)
    # The floodfill will target near-white pixels and fill them with a specific color
    # Then we replace that specific color with transparent
    
    # First, let's create a temporary marker color (e.g. magenta with alpha 1)
    marker_color = (255, 0, 255, 1)
    
    # Flood fill all white/near-white pixels connected to (0,0)
    # Pillow's ImageDraw.floodfill allows replacing a color connected to 0,0
    # Actually, the background might not be perfectly uniform, so maybe a threshold is needed.
    # ImageDraw.floodfill has a difference parameter in newer Pillow versions.
    ImageDraw.floodfill(img, (0, 0), marker_color, thresh=30)
    ImageDraw.floodfill(img, (img.width-1, 0), marker_color, thresh=30)
    ImageDraw.floodfill(img, (0, img.height-1), marker_color, thresh=30)
    ImageDraw.floodfill(img, (img.width-1, img.height-1), marker_color, thresh=30)

    # Now make the marker color fully transparent
    datas = img.getdata()
    newData = []
    for item in datas:
        if item == marker_color:
            newData.append((255, 255, 255, 0)) # transparent
        else:
            newData.append(item)
            
    img.putdata(newData)
    img.save(output_path, "PNG")

icons = ["icon16.png", "icon32.png", "icon48.png", "icon128.png", "icon-feather.png", "icon-phoenix.png"]
for icon in icons:
    print(f"Processing {icon}...")
    make_transparent_edges(icon, icon)

print("Done.")
