Update app.py
Browse files
app.py
CHANGED
@@ -2,57 +2,53 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
-
def
|
6 |
if mask_img is None or bg_img is None:
|
7 |
return None
|
8 |
|
9 |
-
# Convert
|
10 |
mask = mask_img.convert("RGB")
|
11 |
bg = bg_img.convert("RGBA")
|
12 |
mask = mask.resize(bg.size)
|
13 |
|
14 |
-
# Convert
|
15 |
mask_np = np.array(mask).astype(np.float32)
|
16 |
brightness = np.dot(mask_np[..., :3], [0.299, 0.587, 0.114])
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
22 |
|
23 |
-
#
|
24 |
-
overlay_rgba = np.dstack((rgb, alpha)) # shape: (H, W, 4)
|
25 |
-
|
26 |
-
# Convert background to float32
|
27 |
bg_np = np.array(bg).astype(np.float32) / 255.0
|
28 |
|
29 |
-
#
|
30 |
-
alpha_fg =
|
31 |
alpha_bg = bg_np[..., 3:4]
|
32 |
-
|
33 |
-
# Premultiplied blend
|
34 |
out_alpha = alpha_fg + alpha_bg * (1 - alpha_fg)
|
35 |
-
out_rgb = (
|
36 |
|
37 |
-
#
|
38 |
out_img = np.dstack((out_rgb, out_alpha)).clip(0, 1) * 255
|
39 |
-
|
40 |
-
|
41 |
-
return final
|
42 |
|
43 |
# Gradio UI
|
44 |
iface = gr.Interface(
|
45 |
-
fn=
|
46 |
inputs=[
|
47 |
-
gr.Image(type="pil", label="Mask Image (
|
48 |
-
gr.Image(type="pil", label="Background Image (
|
49 |
-
gr.Checkbox(label="Invert Brightness", value=False)
|
50 |
],
|
51 |
-
outputs=gr.Image(type="pil", label="Composite
|
52 |
-
title="
|
53 |
description=(
|
54 |
-
"
|
55 |
-
"and overlays it
|
56 |
)
|
57 |
)
|
58 |
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
+
def white_alpha_overlay(mask_img, bg_img, invert_opacity=False):
|
6 |
if mask_img is None or bg_img is None:
|
7 |
return None
|
8 |
|
9 |
+
# Convert and resize
|
10 |
mask = mask_img.convert("RGB")
|
11 |
bg = bg_img.convert("RGBA")
|
12 |
mask = mask.resize(bg.size)
|
13 |
|
14 |
+
# Convert brightness to alpha
|
15 |
mask_np = np.array(mask).astype(np.float32)
|
16 |
brightness = np.dot(mask_np[..., :3], [0.299, 0.587, 0.114])
|
17 |
+
if invert_opacity:
|
18 |
+
brightness = 255 - brightness
|
19 |
+
alpha = brightness.clip(0, 255) / 255.0 # 0-1
|
20 |
|
21 |
+
# Create solid white RGBA with computed alpha
|
22 |
+
white = np.ones_like(mask_np) / 255.0 # RGB = 1.0 (white)
|
23 |
+
white_rgba = np.dstack((white, alpha)) # shape (H, W, 4)
|
24 |
|
25 |
+
# Background as float
|
|
|
|
|
|
|
26 |
bg_np = np.array(bg).astype(np.float32) / 255.0
|
27 |
|
28 |
+
# Blend overlay on top of background
|
29 |
+
alpha_fg = white_rgba[..., 3:4]
|
30 |
alpha_bg = bg_np[..., 3:4]
|
|
|
|
|
31 |
out_alpha = alpha_fg + alpha_bg * (1 - alpha_fg)
|
32 |
+
out_rgb = (white_rgba[..., :3] * alpha_fg + bg_np[..., :3] * alpha_bg * (1 - alpha_fg)) / np.maximum(out_alpha, 1e-6)
|
33 |
|
34 |
+
# Final image
|
35 |
out_img = np.dstack((out_rgb, out_alpha)).clip(0, 1) * 255
|
36 |
+
result = Image.fromarray(out_img.astype(np.uint8), mode="RGBA")
|
37 |
+
return result
|
|
|
38 |
|
39 |
# Gradio UI
|
40 |
iface = gr.Interface(
|
41 |
+
fn=white_alpha_overlay,
|
42 |
inputs=[
|
43 |
+
gr.Image(type="pil", label="Mask Image (Used for Brightness → Alpha)"),
|
44 |
+
gr.Image(type="pil", label="Background Image (With Alpha OK)"),
|
45 |
+
gr.Checkbox(label="Invert Brightness to Alpha", value=False)
|
46 |
],
|
47 |
+
outputs=gr.Image(type="pil", label="White Overlay Composite"),
|
48 |
+
title="🕊️ Brightness-to-Alpha White Overlay",
|
49 |
description=(
|
50 |
+
"Converts the brightness of the top image into opacity, sets the image color to white, "
|
51 |
+
"and overlays it on a background. Works with full transparency."
|
52 |
)
|
53 |
)
|
54 |
|