Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
bg = bg_img.convert("RGBA").resize(top.size)
|
12 |
-
top_np = np.array(top).astype(np.float32)
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
if invert_opacity:
|
17 |
-
brightness = 255 - brightness
|
18 |
-
alpha_top = (brightness / 255.0).clip(0, 1) # shape (H, W)
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
#
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
-
height, width = combined_alpha.shape
|
29 |
-
white_rgb = np.ones((height, width, 3), dtype=np.float32) # all white
|
30 |
-
out_rgba = np.dstack((white_rgb, combined_alpha)) * 255
|
31 |
-
out_img = Image.fromarray(out_rgba.astype(np.uint8), mode="RGBA")
|
32 |
-
return out_img
|
33 |
-
|
34 |
-
# Gradio UI
|
35 |
iface = gr.Interface(
|
36 |
-
fn=
|
37 |
-
inputs=
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
],
|
42 |
-
outputs=gr.Image(type="pil", label="Combined Opacity Image"),
|
43 |
-
title="🔁 Combine Two Opacity Images (Multiply Alphas)",
|
44 |
-
description=(
|
45 |
-
"Takes two images and multiplies their opacity (alpha). Output is solid white "
|
46 |
-
"with combined alpha values. Useful for stacking brightness masks."
|
47 |
-
)
|
48 |
)
|
49 |
|
|
|
50 |
if __name__ == "__main__":
|
51 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
+
import io
|
5 |
|
6 |
+
def brightness_to_opacity(image):
|
7 |
+
# Ensure it's RGB
|
8 |
+
img = image.convert("RGB")
|
9 |
+
img_array = np.array(img)
|
10 |
|
11 |
+
# Compute grayscale (luminance)
|
12 |
+
gray = np.dot(img_array[...,:3], [0.299, 0.587, 0.114]).astype(np.uint8)
|
|
|
|
|
13 |
|
14 |
+
# Use grayscale as alpha
|
15 |
+
alpha = gray
|
|
|
|
|
|
|
16 |
|
17 |
+
# Optional: set RGB to white (or use img_array to preserve color)
|
18 |
+
white_rgb = np.ones_like(img_array) * 255
|
19 |
+
rgba_array = np.dstack((white_rgb, alpha))
|
20 |
|
21 |
+
# Convert to Image and return
|
22 |
+
result_img = Image.fromarray(rgba_array.astype(np.uint8), mode="RGBA")
|
23 |
+
return result_img
|
24 |
|
25 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
iface = gr.Interface(
|
27 |
+
fn=brightness_to_opacity,
|
28 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
29 |
+
outputs=gr.Image(type="pil", label="Output (Brightness → Opacity)"),
|
30 |
+
title="Brightness to Opacity Converter",
|
31 |
+
description="This tool converts image brightness into opacity. Bright areas become visible, dark areas become transparent."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
|
34 |
+
# Launch the app
|
35 |
if __name__ == "__main__":
|
36 |
iface.launch()
|