Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
from rembg import remove
|
3 |
-
from PIL import Image
|
4 |
import os
|
|
|
|
|
5 |
|
6 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Remove the background from the image
|
8 |
output = remove(image)
|
9 |
|
|
|
|
|
|
|
|
|
10 |
# Save the image with the specified filename
|
11 |
if not filename.endswith(".png"):
|
12 |
filename += ".png"
|
@@ -16,19 +35,30 @@ def remove_background(image, filename):
|
|
16 |
|
17 |
return output, output_path
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Create the Gradio interface
|
20 |
iface = gr.Interface(
|
21 |
fn=remove_background,
|
22 |
inputs=[
|
23 |
gr.Image(type="pil", label="Input Image"),
|
24 |
-
gr.Textbox(label="Filename (without extension)", placeholder="Enter filename")
|
|
|
25 |
],
|
26 |
outputs=[
|
27 |
gr.Image(type="pil", label="Background-Removed Image"),
|
28 |
gr.File(label="Download Image")
|
29 |
],
|
30 |
title="RemBgV0", # Application title
|
31 |
-
description="Upload an image
|
|
|
|
|
|
|
32 |
)
|
33 |
|
34 |
# Launch the application
|
|
|
1 |
import gradio as gr
|
2 |
from rembg import remove
|
3 |
+
from PIL import Image, ImageEnhance
|
4 |
import os
|
5 |
+
import requests
|
6 |
+
from io import BytesIO
|
7 |
|
8 |
+
def enhance_image(image):
|
9 |
+
"""Enhance the image by adjusting sharpness, contrast, and brightness."""
|
10 |
+
enhancer = ImageEnhance.Sharpness(image)
|
11 |
+
image = enhancer.enhance(2.0) # Increase sharpness
|
12 |
+
|
13 |
+
enhancer = ImageEnhance.Contrast(image)
|
14 |
+
image = enhancer.enhance(1.2) # Increase contrast
|
15 |
+
|
16 |
+
enhancer = ImageEnhance.Brightness(image)
|
17 |
+
image = enhancer.enhance(1.1) # Increase brightness
|
18 |
+
|
19 |
+
return image
|
20 |
+
|
21 |
+
def remove_background(image, filename, enhance):
|
22 |
# Remove the background from the image
|
23 |
output = remove(image)
|
24 |
|
25 |
+
# Enhance the image after removing the background (if enabled)
|
26 |
+
if enhance:
|
27 |
+
output = enhance_image(output)
|
28 |
+
|
29 |
# Save the image with the specified filename
|
30 |
if not filename.endswith(".png"):
|
31 |
filename += ".png"
|
|
|
35 |
|
36 |
return output, output_path
|
37 |
|
38 |
+
# Load the example image from the URL
|
39 |
+
def load_example_image():
|
40 |
+
url = "https://huggingface.co/spaces/Doubleupai/RemBgV0/blob/main/example.jpg"
|
41 |
+
response = requests.get(url)
|
42 |
+
image = Image.open(BytesIO(response.content))
|
43 |
+
return image
|
44 |
+
|
45 |
# Create the Gradio interface
|
46 |
iface = gr.Interface(
|
47 |
fn=remove_background,
|
48 |
inputs=[
|
49 |
gr.Image(type="pil", label="Input Image"),
|
50 |
+
gr.Textbox(label="Filename (without extension)", placeholder="Enter filename"),
|
51 |
+
gr.Checkbox(label="Enhance Image After Background Removal", value=True) # Checkbox for enhancement
|
52 |
],
|
53 |
outputs=[
|
54 |
gr.Image(type="pil", label="Background-Removed Image"),
|
55 |
gr.File(label="Download Image")
|
56 |
],
|
57 |
title="RemBgV0", # Application title
|
58 |
+
description="Upload an image, specify a filename, and choose whether to enhance the image after removing the background.",
|
59 |
+
examples=[
|
60 |
+
[load_example_image(), "example_output", True] # Example image, filename, and enhancement option
|
61 |
+
]
|
62 |
)
|
63 |
|
64 |
# Launch the application
|