siyamkhan commited on
Commit
f2d943b
·
verified ·
1 Parent(s): 4aefaf5

Uploaded the app.py file which contains the logic

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+
5
+ def custom_composite(im):
6
+ # Get image dimensions and check the number of channels
7
+ h, w, c = im["background"].shape # c could be 3 (RGB) or 4 (RGBA)
8
+
9
+ # Create a black background with 3 channels (RGB)
10
+ black_bg = np.zeros((h, w, 3), dtype=np.uint8)
11
+
12
+ # Extract layers, ensuring they have 3 channels (RGB)
13
+ def to_rgb(layer):
14
+ return (
15
+ layer[:, :, :3] if layer.shape[2] == 4 else layer
16
+ ) # Drop alpha if present
17
+
18
+ layer1 = to_rgb(im["layers"][0]) if len(im["layers"]) > 0 else black_bg
19
+ layer2 = to_rgb(im["layers"][1]) if len(im["layers"]) > 1 else black_bg
20
+
21
+ # Merge layers onto the black background
22
+ final_composite = np.where(layer1 > 0, layer1, black_bg)
23
+ final_composite = np.where(layer2 > 0, layer2, final_composite)
24
+
25
+ return final_composite
26
+
27
+
28
+ with gr.Blocks() as demo:
29
+ with gr.Row():
30
+ im = gr.ImageEditor(
31
+ type="numpy", crop_size="1:1", brush=gr.Brush(default_color="white")
32
+ )
33
+ im_preview = gr.Image(format="png")
34
+
35
+ im.change(custom_composite, outputs=im_preview, inputs=im, show_progress="hidden")
36
+
37
+ if __name__ == "__main__":
38
+ demo.launch()