File size: 1,659 Bytes
4cb282e
1cb76bb
4cb282e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cb76bb
 
 
 
4cb282e
1cb76bb
4cb282e
 
 
 
 
 
 
 
 
1cb76bb
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
import streamlit as st
from rembg import remove, new_session
from PIL import Image
import io
import fitz  # PyMuPDF for handling PDFs

st.set_page_config(page_title="Background Remover", page_icon="🎨", layout="centered")

st.title("🎨 Background Remover - High Quality")
st.markdown("Upload an image (PNG, JPEG, JPG, WEBP, or PDF) and remove the background cleanly!")

# File uploader
uploaded_file = st.file_uploader("Upload an image or PDF", type=["png", "jpeg", "jpg", "webp", "pdf"])

if uploaded_file is not None:
    file_type = uploaded_file.type

    if file_type == "application/pdf":
        # Convert PDF to image
        doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
        page = doc.load_page(0)  # Only process the first page
        image = Image.frombytes("RGB", [int(page.rect.width), int(page.rect.height)], page.get_pixmap().samples)
    else:
        image = Image.open(uploaded_file)

    st.image(image, caption="Original Image", use_column_width=True)

    # Create a high-quality model session
    session = new_session("u2net_hq")  # Load the HQ model

    # Remove background using the HQ model
    with st.spinner("Removing background with high quality..."):
        output_image = remove(image, session=session)

    st.image(output_image, caption="Background Removed", use_column_width=True)

    # Download button
    img_byte_arr = io.BytesIO()
    output_image.save(img_byte_arr, format="PNG")
    st.download_button("Download Image", img_byte_arr.getvalue(), file_name="no_bg_image.png", mime="image/png")

st.markdown("---")
st.markdown("💡 Developed with ❤️ using Streamlit and rembg (U2Net HQ)")