File size: 1,662 Bytes
0a0c980 af7ddb8 847f7e0 c4a46bc af7ddb8 c4a46bc 847f7e0 8ef9afa af7ddb8 847f7e0 c4a46bc 8ef9afa | 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 | import streamlit as st
from rembg import remove, new_session
from PIL import Image, ImageFilter
import io
# Load high-quality model
session = new_session("u2net")
# Streamlit App Configuration
st.set_page_config(page_title="Background Remover & Changer", layout="wide")
st.title("Background Remover & Changer")
# Upload multiple images
uploaded_files = st.file_uploader("Upload Images (Multiple Allowed)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
# Upload custom background
bg_image = st.file_uploader("Upload Custom Background (Optional)", type=["png", "jpg", "jpeg"])
# Process images
if uploaded_files:
st.subheader("Processed Images")
for uploaded_file in uploaded_files:
image = Image.open(uploaded_file)
image = image.convert("RGBA")
# Remove background using high-quality model
removed_bg = remove(image, session=session)
# Apply smoothing to improve edges
removed_bg = removed_bg.filter(ImageFilter.SMOOTH)
if bg_image:
bg = Image.open(bg_image).convert("RGBA")
bg = bg.resize(removed_bg.size)
final_image = Image.alpha_composite(bg, removed_bg)
else:
final_image = removed_bg
st.image(final_image, caption="Processed Image", use_column_width=True)
# Download button
img_bytes = io.BytesIO()
final_image.save(img_bytes, format='PNG')
st.download_button(label="Download Image", data=img_bytes.getvalue(), file_name="processed_image.png", mime="image/png")
st.write("Upload images to remove background and optionally change it.") |