import gradio as gr from PIL import Image import numpy as np import io def brightness_to_opacity(image): # Ensure it's RGB img = image.convert("RGB") img_array = np.array(img) # Compute grayscale (luminance) gray = np.dot(img_array[...,:3], [0.299, 0.587, 0.114]).astype(np.uint8) # Use grayscale as alpha alpha = gray # Optional: set RGB to white (or use img_array to preserve color) white_rgb = np.ones_like(img_array) * 255 rgba_array = np.dstack((white_rgb, alpha)) # Convert to Image and return result_img = Image.fromarray(rgba_array.astype(np.uint8), mode="RGBA") return result_img # Gradio Interface iface = gr.Interface( fn=brightness_to_opacity, inputs=gr.Image(type="pil", label="Upload Image"), outputs=gr.Image(type="pil", label="Output (Brightness → Opacity)"), title="Brightness to Opacity Converter", description="This tool converts image brightness into opacity. Bright areas become visible, dark areas become transparent." ) # Launch the app if __name__ == "__main__": iface.launch()