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