muqeet1234's picture
Update app.py
b5010d3 verified
Raw
History Blame Contribute Delete
2.19 kB
import streamlit as st
from PIL import Image
from io import BytesIO
from rembg import remove
# Set page configuration
st.set_page_config(page_title="Background Remover App", page_icon="🎨", layout="centered")
# Title and description
st.title("✨ Background Remover App")
st.write("Easily remove the background from your images with just a few clicks. Upload any image format, and let the app do the magic! 🚀")
# Sidebar for instructions
st.sidebar.title("How to use")
st.sidebar.write("""
1. **Upload an Image**: Use the 'Upload Image' button to upload your file.
2. **Remove Background**: Click the 'Remove Background' button to process the image.
3. **Download Result**: Save the image with a transparent background.
""")
# Upload image
uploaded_file = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg", "bmp", "webp"], label_visibility="collapsed")
if uploaded_file:
# Display uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
else:
st.info("Please upload an image to get started.")
# Remove background button
if uploaded_file:
if st.button("Remove Background"):
with st.spinner("Processing..."):
try:
# Convert the uploaded file into bytes
img_bytes = BytesIO()
image.save(img_bytes, format=image.format) # Save the image in its original format
img_bytes = img_bytes.getvalue()
# Process the image with rembg
output_image = remove(img_bytes)
# Convert the processed bytes back into an image
result_image = Image.open(BytesIO(output_image))
# Display the result
st.image(result_image, caption="Background Removed", use_column_width=True)
# Provide download option
st.download_button(
"Download Image",
data=output_image,
file_name="background_removed.png",
mime="image/png",
)
except Exception as e:
st.error(f"An error occurred: {e}")