TIMBOVILL commited on
Commit
a8c3caa
Β·
verified Β·
1 Parent(s): 182904b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -2,43 +2,45 @@ import gradio as gr
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__":
 
2
  from PIL import Image
3
  import numpy as np
4
 
5
+ def brightness_to_white_alpha(img, bg_img=None, invert_opacity=False):
6
  if img is None:
7
  return None
8
 
9
+ # Step 1: Convert input to RGB and compute brightness
10
  img_rgb = img.convert("RGB")
11
  img_np = np.array(img_rgb).astype(np.float32)
12
+ brightness = np.dot(img_np[..., :3], [0.299, 0.587, 0.114])
13
 
 
 
 
 
14
  if invert_opacity:
15
  brightness = 255 - brightness
16
 
 
17
  alpha = brightness.clip(0, 255).astype(np.uint8)
18
 
19
+ # Step 2: Create solid white RGBA image
20
  height, width = alpha.shape
21
+ white_rgb = np.full((height, width, 3), 255, dtype=np.uint8)
22
+ white_rgba = np.dstack((white_rgb, alpha)) # shape (H, W, 4)
23
+ white_img = Image.fromarray(white_rgba, mode="RGBA")
24
+
25
+ # Step 3: Overlay white image on background (if provided)
26
+ if bg_img:
27
+ bg = bg_img.convert("RGBA").resize(white_img.size)
28
+ composite = Image.alpha_composite(bg, white_img)
29
+ return composite
30
 
31
+ return white_img
 
 
 
32
 
33
  # Gradio UI
34
  iface = gr.Interface(
35
  fn=brightness_to_white_alpha,
36
  inputs=[
37
  gr.Image(type="pil", label="Input Image"),
38
+ gr.Image(type="pil", label="Optional Background Image"),
39
  gr.Checkbox(label="Invert Brightness β†’ Alpha", value=False)
40
  ],
41
+ outputs=gr.Image(type="pil", label="Final Output"),
42
+ title="πŸ’‘ White Brightness Layer + Optional Background",
43
+ description="Generates a pure white image with brightness as transparency. Optionally overlays it on a background."
44
  )
45
 
46
  if __name__ == "__main__":