Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,42 +3,32 @@ import numpy as np
|
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
os.makedirs("examples", exist_ok=True)
|
| 9 |
|
| 10 |
def refine_edges(image, edge_smoothness=3, blur_radius=2, feather_amount=1):
|
| 11 |
"""
|
| 12 |
Refines edges of a transparent PNG image with configurable parameters.
|
| 13 |
-
|
| 14 |
-
Args:
|
| 15 |
-
image: Input image (PIL Image)
|
| 16 |
-
edge_smoothness: Intensity of edge refinement (1-5)
|
| 17 |
-
blur_radius: Kernel size for edge smoothing (1-5)
|
| 18 |
-
feather_amount: Feathering amount at transparency edges (0-5)
|
| 19 |
-
|
| 20 |
-
Returns:
|
| 21 |
-
Refined PIL Image
|
| 22 |
"""
|
| 23 |
img = image.convert("RGBA")
|
| 24 |
np_img = np.array(img)
|
| 25 |
alpha = np_img[:, :, 3]
|
| 26 |
|
| 27 |
# Scale parameters
|
| 28 |
-
blur_kernel = blur_radius * 2 + 1
|
| 29 |
smooth_iterations = edge_smoothness
|
| 30 |
feather_size = feather_amount
|
| 31 |
|
| 32 |
-
# Edge smoothing
|
| 33 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 34 |
for _ in range(smooth_iterations):
|
| 35 |
alpha = cv2.morphologyEx(alpha, cv2.MORPH_OPEN, kernel)
|
| 36 |
alpha = cv2.morphologyEx(alpha, cv2.MORPH_CLOSE, kernel)
|
| 37 |
|
| 38 |
-
# Gaussian blur
|
| 39 |
alpha = cv2.GaussianBlur(alpha, (blur_kernel, blur_kernel), 0)
|
| 40 |
|
| 41 |
-
# Feather edges
|
| 42 |
if feather_amount > 0:
|
| 43 |
_, mask = cv2.threshold(alpha, 10, 255, cv2.THRESH_BINARY)
|
| 44 |
edges = cv2.Canny(mask, 100, 200)
|
|
@@ -46,56 +36,64 @@ def refine_edges(image, edge_smoothness=3, blur_radius=2, feather_amount=1):
|
|
| 46 |
alpha_blurred = cv2.GaussianBlur(alpha, (blur_kernel, blur_kernel), 0)
|
| 47 |
alpha = np.where(edges > 0, alpha_blurred, alpha)
|
| 48 |
|
| 49 |
-
# Re-normalize alpha
|
| 50 |
alpha = np.clip(alpha, 0, 255).astype(np.uint8)
|
| 51 |
np_img[:, :, 3] = alpha
|
| 52 |
|
| 53 |
return Image.fromarray(np_img)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Create Gradio interface
|
| 71 |
-
with gr.Blocks() as demo:
|
| 72 |
gr.Markdown("""
|
| 73 |
# ✨ Edge Refiner - Clean Up Your Background-Removed Images!
|
| 74 |
-
Refine the edges of
|
| 75 |
""")
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
with gr.Column():
|
| 79 |
-
input_image = gr.Image(type="pil", label="Input Image
|
| 80 |
edge_smoothness = gr.Slider(1, 5, value=3, step=1, label="Edge Smoothness")
|
| 81 |
blur_radius = gr.Slider(1, 5, value=2, step=1, label="Blur Radius")
|
| 82 |
feather_amount = gr.Slider(0, 5, value=1, step=1, label="Feather Amount")
|
| 83 |
-
submit_btn = gr.Button("Refine Edges")
|
| 84 |
|
| 85 |
with gr.Column():
|
| 86 |
output_image = gr.Image(type="pil", label="Refined Image")
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
["examples/
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)
|
| 99 |
|
| 100 |
submit_btn.click(
|
| 101 |
fn=refine_edges,
|
|
@@ -104,4 +102,4 @@ with gr.Blocks() as demo:
|
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
-
demo.launch()
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import requests
|
|
|
|
| 8 |
|
| 9 |
def refine_edges(image, edge_smoothness=3, blur_radius=2, feather_amount=1):
|
| 10 |
"""
|
| 11 |
Refines edges of a transparent PNG image with configurable parameters.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
img = image.convert("RGBA")
|
| 14 |
np_img = np.array(img)
|
| 15 |
alpha = np_img[:, :, 3]
|
| 16 |
|
| 17 |
# Scale parameters
|
| 18 |
+
blur_kernel = blur_radius * 2 + 1
|
| 19 |
smooth_iterations = edge_smoothness
|
| 20 |
feather_size = feather_amount
|
| 21 |
|
| 22 |
+
# Edge smoothing
|
| 23 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 24 |
for _ in range(smooth_iterations):
|
| 25 |
alpha = cv2.morphologyEx(alpha, cv2.MORPH_OPEN, kernel)
|
| 26 |
alpha = cv2.morphologyEx(alpha, cv2.MORPH_CLOSE, kernel)
|
| 27 |
|
| 28 |
+
# Gaussian blur
|
| 29 |
alpha = cv2.GaussianBlur(alpha, (blur_kernel, blur_kernel), 0)
|
| 30 |
|
| 31 |
+
# Feather edges
|
| 32 |
if feather_amount > 0:
|
| 33 |
_, mask = cv2.threshold(alpha, 10, 255, cv2.THRESH_BINARY)
|
| 34 |
edges = cv2.Canny(mask, 100, 200)
|
|
|
|
| 36 |
alpha_blurred = cv2.GaussianBlur(alpha, (blur_kernel, blur_kernel), 0)
|
| 37 |
alpha = np.where(edges > 0, alpha_blurred, alpha)
|
| 38 |
|
|
|
|
| 39 |
alpha = np.clip(alpha, 0, 255).astype(np.uint8)
|
| 40 |
np_img[:, :, 3] = alpha
|
| 41 |
|
| 42 |
return Image.fromarray(np_img)
|
| 43 |
|
| 44 |
+
def download_example_images():
|
| 45 |
+
"""Download example images from alternative sources"""
|
| 46 |
+
example_images = {
|
| 47 |
+
"hair.png": "https://i.imgur.com/JQJQJQJ.png", # Replace with actual URL
|
| 48 |
+
"furry_animal.png": "https://i.imgur.com/ANIMAL.png",
|
| 49 |
+
"glasses.png": "https://i.imgur.com/GLASSES.png"
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
os.makedirs("examples", exist_ok=True)
|
| 53 |
+
|
| 54 |
+
for filename, url in example_images.items():
|
| 55 |
try:
|
| 56 |
+
response = requests.get(url)
|
| 57 |
+
if response.status_code == 200:
|
| 58 |
+
with open(f"examples/{filename}", "wb") as f:
|
| 59 |
+
f.write(response.content)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Couldn't download {filename}: {str(e)}")
|
| 62 |
+
# Provide fallback blank image
|
| 63 |
+
blank = Image.new("RGBA", (256, 256), (0, 0, 0, 0))
|
| 64 |
+
blank.save(f"examples/{filename}")
|
| 65 |
+
|
| 66 |
+
# Download examples at startup
|
| 67 |
+
download_example_images()
|
| 68 |
|
| 69 |
# Create Gradio interface
|
| 70 |
+
with gr.Blocks(title="✨ Edge Refiner") as demo:
|
| 71 |
gr.Markdown("""
|
| 72 |
# ✨ Edge Refiner - Clean Up Your Background-Removed Images!
|
| 73 |
+
Refine the edges of transparent PNG images for cleaner results.
|
| 74 |
""")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column():
|
| 78 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
| 79 |
edge_smoothness = gr.Slider(1, 5, value=3, step=1, label="Edge Smoothness")
|
| 80 |
blur_radius = gr.Slider(1, 5, value=2, step=1, label="Blur Radius")
|
| 81 |
feather_amount = gr.Slider(0, 5, value=1, step=1, label="Feather Amount")
|
| 82 |
+
submit_btn = gr.Button("Refine Edges", variant="primary")
|
| 83 |
|
| 84 |
with gr.Column():
|
| 85 |
output_image = gr.Image(type="pil", label="Refined Image")
|
| 86 |
|
| 87 |
+
# Use local example files
|
| 88 |
+
example_files = [f for f in os.listdir("examples") if f.endswith(".png")]
|
| 89 |
+
if example_files:
|
| 90 |
+
gr.Examples(
|
| 91 |
+
examples=[[f"examples/{f}", 3, 2, 1] for f in example_files],
|
| 92 |
+
inputs=[input_image, edge_smoothness, blur_radius, feather_amount],
|
| 93 |
+
outputs=output_image,
|
| 94 |
+
fn=refine_edges,
|
| 95 |
+
cache_examples=False
|
| 96 |
+
)
|
|
|
|
| 97 |
|
| 98 |
submit_btn.click(
|
| 99 |
fn=refine_edges,
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|