Update app.py
Browse files
app.py
CHANGED
@@ -2,60 +2,43 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
-
def brightness_to_white_alpha(img,
|
6 |
if img is None:
|
7 |
return None
|
8 |
|
9 |
-
# Convert
|
10 |
-
|
11 |
-
img_np = np.array(
|
|
|
|
|
12 |
brightness = np.dot(img_np[..., :3], [0.299, 0.587, 0.114]) # shape: (H, W)
|
13 |
|
|
|
14 |
if invert_opacity:
|
15 |
brightness = 255 - brightness
|
16 |
|
17 |
-
#
|
18 |
-
alpha =
|
19 |
-
alpha = (alpha * 255).astype(np.uint8)
|
20 |
-
|
21 |
-
# Create pure white RGB
|
22 |
-
white = np.ones_like(img_np, dtype=np.uint8) * 255 # shape: (H, W, 3)
|
23 |
-
|
24 |
-
# Combine white RGB and computed alpha
|
25 |
-
white_alpha = np.dstack((white, alpha)).astype(np.uint8) # shape: (H, W, 4)
|
26 |
-
|
27 |
-
white_img = Image.fromarray(white_alpha, mode="RGBA")
|
28 |
-
|
29 |
-
# If background is provided, overlay white on top
|
30 |
-
if bg:
|
31 |
-
bg = bg.convert("RGBA").resize(white_img.size)
|
32 |
-
white_np = np.array(white_img).astype(np.float32) / 255.0
|
33 |
-
bg_np = np.array(bg).astype(np.float32) / 255.0
|
34 |
-
|
35 |
-
fg_a = white_np[..., 3:4]
|
36 |
-
bg_a = bg_np[..., 3:4]
|
37 |
-
out_a = fg_a + bg_a * (1 - fg_a)
|
38 |
-
out_rgb = (white_np[..., :3] * fg_a + bg_np[..., :3] * bg_a * (1 - fg_a)) / np.maximum(out_a, 1e-6)
|
39 |
|
40 |
-
|
41 |
-
|
|
|
42 |
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
# Gradio UI
|
46 |
iface = gr.Interface(
|
47 |
fn=brightness_to_white_alpha,
|
48 |
inputs=[
|
49 |
gr.Image(type="pil", label="Input Image"),
|
50 |
-
gr.Image(type="pil", label="Optional Background Image"),
|
51 |
gr.Checkbox(label="Invert Brightness β Alpha", value=False)
|
52 |
],
|
53 |
-
outputs=gr.Image(type="pil", label="White with Alpha"),
|
54 |
-
title="
|
55 |
-
description=
|
56 |
-
"Generates a pure white image where opacity = brightness of input. "
|
57 |
-
"Darker = more transparent, lighter = more opaque. Optional overlay on background."
|
58 |
-
)
|
59 |
)
|
60 |
|
61 |
if __name__ == "__main__":
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
+
def brightness_to_white_alpha(img, invert_opacity=False):
|
6 |
if img is None:
|
7 |
return None
|
8 |
|
9 |
+
# Step 1: Convert to RGB
|
10 |
+
img_rgb = img.convert("RGB")
|
11 |
+
img_np = np.array(img_rgb).astype(np.float32)
|
12 |
+
|
13 |
+
# Step 2: Calculate brightness (luminance)
|
14 |
brightness = np.dot(img_np[..., :3], [0.299, 0.587, 0.114]) # shape: (H, W)
|
15 |
|
16 |
+
# Step 3: Invert if requested
|
17 |
if invert_opacity:
|
18 |
brightness = 255 - brightness
|
19 |
|
20 |
+
# Step 4: Normalize to 0β255 alpha
|
21 |
+
alpha = brightness.clip(0, 255).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Step 5: Create solid white RGB image
|
24 |
+
height, width = alpha.shape
|
25 |
+
white_rgb = np.full((height, width, 3), 255, dtype=np.uint8) # White RGB
|
26 |
|
27 |
+
# Step 6: Combine into final RGBA image
|
28 |
+
white_rgba = np.dstack((white_rgb, alpha)) # shape: (H, W, 4)
|
29 |
+
out_img = Image.fromarray(white_rgba, mode="RGBA")
|
30 |
+
return out_img
|
31 |
|
32 |
# Gradio UI
|
33 |
iface = gr.Interface(
|
34 |
fn=brightness_to_white_alpha,
|
35 |
inputs=[
|
36 |
gr.Image(type="pil", label="Input Image"),
|
|
|
37 |
gr.Checkbox(label="Invert Brightness β Alpha", value=False)
|
38 |
],
|
39 |
+
outputs=gr.Image(type="pil", label="White Image with Brightness-Based Alpha"),
|
40 |
+
title="ποΈ Brightness to Alpha (White Glow Generator)",
|
41 |
+
description="Generates a pure white image where transparency is based on brightness β dark = transparent, light = opaque."
|
|
|
|
|
|
|
42 |
)
|
43 |
|
44 |
if __name__ == "__main__":
|