Update app.py
Browse files
app.py
CHANGED
@@ -2,53 +2,53 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
-
def
|
6 |
-
if
|
7 |
return None
|
8 |
|
9 |
-
#
|
10 |
-
mask = mask_img.convert("RGB")
|
11 |
bg = bg_img.convert("RGBA")
|
12 |
-
|
13 |
|
14 |
-
# Convert
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
if invert_opacity:
|
18 |
brightness = 255 - brightness
|
19 |
-
alpha = brightness.clip(0,
|
20 |
|
21 |
-
# Create solid white
|
22 |
-
white = np.ones_like(
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
bg_np = np.array(bg).astype(np.float32) / 255.0
|
27 |
|
28 |
-
# Blend
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
out_rgb = (
|
33 |
|
34 |
-
# Final
|
35 |
-
|
36 |
-
|
37 |
-
return result
|
38 |
|
39 |
# Gradio UI
|
40 |
iface = gr.Interface(
|
41 |
-
fn=
|
42 |
inputs=[
|
43 |
-
gr.Image(type="pil", label="
|
44 |
-
gr.Image(type="pil", label="Background Image (
|
45 |
-
gr.Checkbox(label="Invert Brightness
|
46 |
],
|
47 |
-
outputs=gr.Image(type="pil", label="
|
48 |
-
title="
|
49 |
description=(
|
50 |
-
"
|
51 |
-
"
|
52 |
)
|
53 |
)
|
54 |
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
+
def white_from_brightness_overlay(fg_img, bg_img, invert_opacity=False):
|
6 |
+
if fg_img is None or bg_img is None:
|
7 |
return None
|
8 |
|
9 |
+
# Resize fg to match bg
|
|
|
10 |
bg = bg_img.convert("RGBA")
|
11 |
+
fg = fg_img.convert("RGB").resize(bg.size)
|
12 |
|
13 |
+
# Convert to NumPy arrays
|
14 |
+
fg_np = np.array(fg).astype(np.float32)
|
15 |
+
bg_np = np.array(bg).astype(np.float32) / 255.0
|
16 |
+
|
17 |
+
# Calculate brightness
|
18 |
+
brightness = np.dot(fg_np[..., :3], [0.299, 0.587, 0.114]) # shape (H, W)
|
19 |
if invert_opacity:
|
20 |
brightness = 255 - brightness
|
21 |
+
alpha = (brightness / 255.0).clip(0, 1) # shape (H, W)
|
22 |
|
23 |
+
# Create solid white with alpha from brightness
|
24 |
+
white = np.ones_like(fg_np) # 255,255,255
|
25 |
+
white_alpha = np.dstack((white, alpha * 255)).astype(np.uint8) # shape (H, W, 4)
|
26 |
+
white_img = Image.fromarray(white_alpha, mode="RGBA")
|
27 |
+
white_np = np.array(white_img).astype(np.float32) / 255.0
|
|
|
28 |
|
29 |
+
# Blend white image over background using premultiplied alpha
|
30 |
+
fg_a = white_np[..., 3:4]
|
31 |
+
bg_a = bg_np[..., 3:4]
|
32 |
+
out_a = fg_a + bg_a * (1 - fg_a)
|
33 |
+
out_rgb = (white_np[..., :3] * fg_a + bg_np[..., :3] * bg_a * (1 - fg_a)) / np.maximum(out_a, 1e-6)
|
34 |
|
35 |
+
# Final output
|
36 |
+
result = np.dstack((out_rgb, out_a)).clip(0, 1) * 255
|
37 |
+
return Image.fromarray(result.astype(np.uint8), mode="RGBA")
|
|
|
38 |
|
39 |
# Gradio UI
|
40 |
iface = gr.Interface(
|
41 |
+
fn=white_from_brightness_overlay,
|
42 |
inputs=[
|
43 |
+
gr.Image(type="pil", label="Input Image (Used for Brightness Only)"),
|
44 |
+
gr.Image(type="pil", label="Background Image (Can Have Transparency)"),
|
45 |
+
gr.Checkbox(label="Invert Brightness", value=False)
|
46 |
],
|
47 |
+
outputs=gr.Image(type="pil", label="Final Output"),
|
48 |
+
title="✨ White Brightness-to-Alpha Overlay",
|
49 |
description=(
|
50 |
+
"Uses the brightness of the input image to generate alpha. RGB is replaced with white. "
|
51 |
+
"Result is a smooth glowing white shape over your background, respecting transparency."
|
52 |
)
|
53 |
)
|
54 |
|