Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,7 +27,7 @@ def fn(image: Union[Image.Image, str]) -> Tuple[Image.Image, Image.Image]:
|
|
| 27 |
Remove the background from an image and return both the transparent version and the original.
|
| 28 |
|
| 29 |
This function performs background removal using a BiRefNet segmentation model. It is intended for use
|
| 30 |
-
with image input (either uploaded or from a URL). The function returns a transparent
|
| 31 |
with the background removed, along with the original RGB version for comparison.
|
| 32 |
|
| 33 |
Args:
|
|
@@ -76,27 +76,27 @@ def process(image: Image.Image) -> Image.Image:
|
|
| 76 |
|
| 77 |
def process_file(f: str) -> str:
|
| 78 |
"""
|
| 79 |
-
Load an image file from disk, remove the background, and save the output as a transparent
|
| 80 |
|
| 81 |
Args:
|
| 82 |
f (str): Filepath of the image to process.
|
| 83 |
|
| 84 |
Returns:
|
| 85 |
-
str: Path to the saved
|
| 86 |
"""
|
| 87 |
-
name_path = f.rsplit(".", 1)[0] + ".
|
| 88 |
im = load_img(f, output_type="pil")
|
| 89 |
im = im.convert("RGB")
|
| 90 |
transparent = process(im)
|
| 91 |
transparent.save(name_path)
|
| 92 |
return name_path
|
| 93 |
|
| 94 |
-
slider1 = gr.ImageSlider(label="Processed Image", type="pil"
|
| 95 |
-
slider2 = gr.ImageSlider(label="Processed Image from URL", type="pil"
|
| 96 |
image_upload = gr.Image(label="Upload an image")
|
| 97 |
image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
| 98 |
url_input = gr.Textbox(label="Paste an image URL")
|
| 99 |
-
output_file = gr.File(label="Output
|
| 100 |
|
| 101 |
# Example images
|
| 102 |
chameleon = load_img("butterfly.jpg", output_type="pil")
|
|
@@ -104,7 +104,7 @@ url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-122989298
|
|
| 104 |
|
| 105 |
tab1 = gr.Interface(fn, inputs=image_upload, outputs=slider1, examples=[chameleon], api_name="image")
|
| 106 |
tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, examples=[url_example], api_name="text")
|
| 107 |
-
tab3 = gr.Interface(process_file, inputs=image_file_upload, outputs=output_file, examples=["butterfly.jpg"], api_name="
|
| 108 |
|
| 109 |
demo = gr.TabbedInterface(
|
| 110 |
[tab1, tab2, tab3], ["Image Upload", "URL Input", "File Output"], title="Background Removal Tool"
|
|
|
|
| 27 |
Remove the background from an image and return both the transparent version and the original.
|
| 28 |
|
| 29 |
This function performs background removal using a BiRefNet segmentation model. It is intended for use
|
| 30 |
+
with image input (either uploaded or from a URL). The function returns a transparent WEBP version of the image
|
| 31 |
with the background removed, along with the original RGB version for comparison.
|
| 32 |
|
| 33 |
Args:
|
|
|
|
| 76 |
|
| 77 |
def process_file(f: str) -> str:
|
| 78 |
"""
|
| 79 |
+
Load an image file from disk, remove the background, and save the output as a transparent WEBP.
|
| 80 |
|
| 81 |
Args:
|
| 82 |
f (str): Filepath of the image to process.
|
| 83 |
|
| 84 |
Returns:
|
| 85 |
+
str: Path to the saved WEBP image with background removed.
|
| 86 |
"""
|
| 87 |
+
name_path = f.rsplit(".", 1)[0] + ".webp"
|
| 88 |
im = load_img(f, output_type="pil")
|
| 89 |
im = im.convert("RGB")
|
| 90 |
transparent = process(im)
|
| 91 |
transparent.save(name_path)
|
| 92 |
return name_path
|
| 93 |
|
| 94 |
+
slider1 = gr.ImageSlider(label="Processed Image", type="pil")
|
| 95 |
+
slider2 = gr.ImageSlider(label="Processed Image from URL", type="pil"
|
| 96 |
image_upload = gr.Image(label="Upload an image")
|
| 97 |
image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
| 98 |
url_input = gr.Textbox(label="Paste an image URL")
|
| 99 |
+
output_file = gr.File(label="Output WEBP File")
|
| 100 |
|
| 101 |
# Example images
|
| 102 |
chameleon = load_img("butterfly.jpg", output_type="pil")
|
|
|
|
| 104 |
|
| 105 |
tab1 = gr.Interface(fn, inputs=image_upload, outputs=slider1, examples=[chameleon], api_name="image")
|
| 106 |
tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, examples=[url_example], api_name="text")
|
| 107 |
+
tab3 = gr.Interface(process_file, inputs=image_file_upload, outputs=output_file, examples=["butterfly.jpg"], api_name="webp")
|
| 108 |
|
| 109 |
demo = gr.TabbedInterface(
|
| 110 |
[tab1, tab2, tab3], ["Image Upload", "URL Input", "File Output"], title="Background Removal Tool"
|