| import streamlit as st |
| from PIL import Image |
| from io import BytesIO |
| from rembg import remove |
|
|
| |
| st.set_page_config(page_title="Background Remover App", page_icon="🎨", layout="centered") |
|
|
| |
| 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! 🚀") |
|
|
| |
| 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. |
| """) |
|
|
| |
| uploaded_file = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg", "bmp", "webp"], label_visibility="collapsed") |
| if uploaded_file: |
| |
| 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.") |
|
|
| |
| if uploaded_file: |
| if st.button("Remove Background"): |
| with st.spinner("Processing..."): |
| try: |
| |
| img_bytes = BytesIO() |
| image.save(img_bytes, format=image.format) |
| img_bytes = img_bytes.getvalue() |
|
|
| |
| output_image = remove(img_bytes) |
|
|
| |
| result_image = Image.open(BytesIO(output_image)) |
|
|
| |
| st.image(result_image, caption="Background Removed", use_column_width=True) |
|
|
| |
| 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}") |
|
|