File size: 1,939 Bytes
b424851
 
 
 
 
 
 
 
deeaa92
 
b424851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 streamlit as st
from rembg import remove
from PIL import Image
import io

# Function to remove background from image
def remove_background(image_bytes):
    input_image = Image.open(io.BytesIO(image_bytes))
    # Rembg expects the image to be in the correct mode, typically RGBA or RGB
    input_image = input_image.convert("RGBA")
    output_image = remove(input_image)
    return output_image

# Streamlit UI setup
st.set_page_config(page_title="Background Remover", layout="wide")

# Title and Description
st.title("Background Removal Tool")
st.markdown("### Upload an image, and we will remove its background for you!")

# Create columns for layout
col1, col2 = st.columns(2)

# Column 1: Image upload
with col1:
    uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])

# Column 2: Instructions
with col2:
    st.markdown("""
    #### Instructions:
    - Upload an image (PNG, JPG, or JPEG) using the button above.
    - The background will be removed automatically.
    - Download the result below once it's ready.
    """)

# When an image is uploaded
if uploaded_file is not None:
    # Show the uploaded image
    st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)

    # Remove the background
    with st.spinner("Removing background..."):
        output_image = remove_background(uploaded_file.read())

    # Show the processed image
    st.image(output_image, caption="Image with Background Removed", use_column_width=True)

    # Allow the user to download the result
    output_image_pil = Image.open(io.BytesIO(output_image))
    buf = io.BytesIO()
    output_image_pil.save(buf, format="PNG")
    byte_im = buf.getvalue()

    st.download_button(
        label="Download Image with Removed Background",
        data=byte_im,
        file_name="image_no_bg.png",
        mime="image/png"
    )

# Footer
st.markdown("---")
st.markdown("Created by [Your Name].")