Janeka commited on
Commit
7dc2911
·
verified ·
1 Parent(s): 4a59fdc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ def refine_edges(image: Image.Image):
7
+ # Convert to OpenCV format (RGBA)
8
+ image_np = np.array(image)
9
+ if image_np.shape[2] == 4:
10
+ alpha_channel = image_np[:, :, 3]
11
+ else:
12
+ return image # Return original if no alpha channel
13
+
14
+ # Step 1: Blur the alpha channel
15
+ blurred = cv2.GaussianBlur(alpha_channel, (5, 5), 0)
16
+
17
+ # Step 2: Morphological refinement
18
+ kernel = np.ones((3,3), np.uint8)
19
+ refined = cv2.morphologyEx(blurred, cv2.MORPH_OPEN, kernel)
20
+
21
+ # Step 3: Recombine channels
22
+ b, g, r = image_np[:, :, 0], image_np[:, :, 1], image_np[:, :, 2]
23
+ rgba = cv2.merge((b, g, r, refined))
24
+
25
+ return Image.fromarray(rgba)
26
+
27
+ iface = gr.Interface(
28
+ fn=refine_edges,
29
+ inputs=gr.Image(type="pil", image_mode="RGBA"),
30
+ outputs="image",
31
+ title="Edge Refiner",
32
+ description="Upload a background-removed PNG and get smoother edges!"
33
+ )
34
+
35
+ if __name__ == "__main__":
36
+ iface.launch()