File size: 1,086 Bytes
5dbde27
 
 
6ac3f0d
5dbde27
6ac3f0d
 
 
 
5dbde27
6ac3f0d
 
182904b
6ac3f0d
 
5dbde27
6ac3f0d
 
 
b1e3e6e
6ac3f0d
 
 
a8c3caa
6ac3f0d
5dbde27
6ac3f0d
 
 
 
 
5dbde27
 
6ac3f0d
5dbde27
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()