Update app.py
Browse files
app.py
CHANGED
@@ -2,56 +2,57 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
-
def brightness_to_opacity_overlay(
|
6 |
-
if
|
7 |
return None
|
8 |
|
9 |
-
# Convert images
|
10 |
-
|
11 |
bg = bg_img.convert("RGBA")
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
luminance = np.dot(fg_np[..., :3], [0.299, 0.587, 0.114])
|
19 |
-
if invert_opacity:
|
20 |
-
luminance = 255 - luminance
|
21 |
-
alpha = (luminance / 255.0).clip(0, 1)
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
overlay_rgba = np.dstack((overlay_rgb, alpha * 255)).astype(np.uint8)
|
26 |
|
27 |
-
# Convert
|
28 |
-
overlay = overlay_rgba.astype(np.float32) / 255.0
|
29 |
bg_np = np.array(bg).astype(np.float32) / 255.0
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
out_a = fg_a + bg_a * (1 - fg_a)
|
35 |
-
out_rgb = (overlay[..., :3] * fg_a + bg_np[..., :3] * bg_a * (1 - fg_a)) / np.maximum(out_a, 1e-6)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Gradio UI
|
43 |
iface = gr.Interface(
|
44 |
fn=brightness_to_opacity_overlay,
|
45 |
inputs=[
|
46 |
-
gr.Image(type="pil", label="
|
47 |
-
gr.Image(type="pil", label="Background Image (
|
48 |
gr.Checkbox(label="Invert Brightness", value=False)
|
49 |
],
|
50 |
-
outputs=gr.Image(type="pil", label="Composite
|
51 |
-
title="
|
52 |
description=(
|
53 |
-
"
|
54 |
-
"and
|
55 |
)
|
56 |
)
|
57 |
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
+
def brightness_to_opacity_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 images and resize mask to match background
|
10 |
+
mask = mask_img.convert("RGB")
|
11 |
bg = bg_img.convert("RGBA")
|
12 |
+
mask = mask.resize(bg.size)
|
13 |
|
14 |
+
# Convert mask to brightness-based alpha
|
15 |
+
mask_np = np.array(mask).astype(np.float32)
|
16 |
+
brightness = np.dot(mask_np[..., :3], [0.299, 0.587, 0.114])
|
17 |
+
alpha = (255 - brightness if invert_opacity else brightness).clip(0, 255)
|
18 |
+
alpha = alpha / 255.0 # normalize to 0-1
|
19 |
|
20 |
+
# Normalize mask RGB
|
21 |
+
rgb = mask_np / 255.0
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Combine original RGB with generated alpha
|
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 |
+
# Extract alphas
|
30 |
+
alpha_fg = overlay_rgba[..., 3:4]
|
31 |
+
alpha_bg = bg_np[..., 3:4]
|
|
|
|
|
32 |
|
33 |
+
# Premultiplied blend
|
34 |
+
out_alpha = alpha_fg + alpha_bg * (1 - alpha_fg)
|
35 |
+
out_rgb = (overlay_rgba[..., :3] * alpha_fg + bg_np[..., :3] * alpha_bg * (1 - alpha_fg)) / np.maximum(out_alpha, 1e-6)
|
36 |
+
|
37 |
+
# Compose final image
|
38 |
+
out_img = np.dstack((out_rgb, out_alpha)).clip(0, 1) * 255
|
39 |
+
final = Image.fromarray(out_img.astype(np.uint8), mode="RGBA")
|
40 |
+
|
41 |
+
return final
|
42 |
|
43 |
# Gradio UI
|
44 |
iface = gr.Interface(
|
45 |
fn=brightness_to_opacity_overlay,
|
46 |
inputs=[
|
47 |
+
gr.Image(type="pil", label="Mask Image (Color OK)"),
|
48 |
+
gr.Image(type="pil", label="Background Image (Supports Alpha)"),
|
49 |
gr.Checkbox(label="Invert Brightness", value=False)
|
50 |
],
|
51 |
+
outputs=gr.Image(type="pil", label="Composite Result"),
|
52 |
+
title="✨ Brightness-to-Alpha with Color Mask",
|
53 |
description=(
|
54 |
+
"Uses the brightness of a colored image as opacity, preserves color, "
|
55 |
+
"and overlays it onto a background image (even with alpha transparency)."
|
56 |
)
|
57 |
)
|
58 |
|