|
import streamlit as st |
|
from PIL import Image |
|
from rembg import remove |
|
import io |
|
import time |
|
|
|
|
|
st.title("🎨 SnipSnap") |
|
st.write("Upload an image, and we'll remove the background for you!") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
input_image = Image.open(uploaded_file) |
|
|
|
|
|
max_width = 600 |
|
width_percent = max_width / float(input_image.size[0]) |
|
height_size = int(float(input_image.size[1]) * float(width_percent)) |
|
input_image = input_image.resize((max_width, height_size), Image.Resampling.LANCZOS) |
|
|
|
|
|
st.image(input_image, caption="Uploaded Image", use_container_width=True) |
|
|
|
|
|
st.write("Removing background...") |
|
progress_bar = st.progress(0) |
|
for i in range(100): |
|
time.sleep(0.02) |
|
progress_bar.progress(i + 1) |
|
|
|
|
|
output_image = remove(input_image) |
|
|
|
|
|
st.image(output_image, caption="Background Removed", use_container_width=True) |
|
|
|
|
|
buffered = io.BytesIO() |
|
output_image.save(buffered, format="PNG") |
|
st.download_button( |
|
label="⬇️ Download Result", |
|
data=buffered.getvalue(), |
|
file_name="background_removed.png", |
|
mime="image/png", |
|
) |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
/* Center-align the title */ |
|
h1 { |
|
text-align: center; |
|
} |
|
|
|
/* Add some padding and a border to the uploader */ |
|
.stFileUploader > div { |
|
padding: 20px; |
|
border: 2px dashed #ccc; |
|
border-radius: 10px; |
|
text-align: center; |
|
margin-top: 20px; |
|
} |
|
|
|
/* Style the download button */ |
|
.stDownloadButton button { |
|
width: 100%; |
|
background-color: #4CAF50; |
|
color: white; |
|
padding: 10px; |
|
border: none; |
|
border-radius: 5px; |
|
font-size: 16px; |
|
} |
|
|
|
/* Style the progress bar */ |
|
.stProgress > div > div > div { |
|
background-color: #4CAF50; |
|
} |
|
</style> |
|
""", |
|
unsafe_allow_html=True, |
|
) |